英文:
Cannot resolve method 'setTimestampsInSnapshotsEnabled' in 'Builder'
问题
我尝试创建一个注册活动,以允许用户注册,但是我一直收到以下错误消息:
> 在 'Builder' 中无法解析方法 'setTimestampsInSnapshotsEnabled'
只有包含 setTimestampsInSnapshotsEnabled
的部分出现了问题。我需要进行实现吗?
以下是我的Java代码:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener
{
private static final String TAG = "RegisterActivity";
//widgets
private EditText mEmail, mPassword, mConfirmPassword;
private ProgressBar mProgressBar;
//vars
private FirebaseFirestore mDb;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mEmail = (EditText) findViewById(R.id.input_email);
mPassword = (EditText) findViewById(R.id.input_password);
mConfirmPassword = (EditText) findViewById(R.id.input_confirm_password);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
findViewById(R.id.btn_register).setOnClickListener(this);
mDb = FirebaseFirestore.getInstance();
hideSoftKeyboard();
}
/**
* 向Firebase Authentication注册新的电子邮件和密码
* @param email
* @param password
*/
public void registerNewEmail(final String email, String password){
showDialog();
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
if (task.isSuccessful()){
Log.d(TAG, "onComplete: AuthState: " + FirebaseAuth.getInstance().getCurrentUser().getUid());
//插入一些默认数据
User user = new User();
user.setEmail(email);
user.setUsername(email.substring(0, email.indexOf("@")));
user.setUser_id(FirebaseAuth.getInstance().getUid());
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
mDb.setFirestoreSettings(settings);
FirebaseFirestoreSettings newUserRef = mDb
.collection(getString(R.string.collection_users))
.document(FirebaseAuth.getInstance().getUid());
newUserRef.set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
hideDialog();
if(task.isSuccessful()){
redirectLoginScreen();
}else{
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "出现了问题。", Snackbar.LENGTH_SHORT).show();
}
}
});
}
else {
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "出现了问题。", Snackbar.LENGTH_SHORT).show();
hideDialog();
}
// ...
}
});
}
/**
* 将用户重定向到登录界面
*/
private void redirectLoginScreen(){
Log.d(TAG, "redirectLoginScreen: 重定向到登录界面。");
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void showDialog(){
mProgressBar.setVisibility(View.VISIBLE);
}
private void hideDialog(){
if(mProgressBar.getVisibility() == View.VISIBLE){
mProgressBar.setVisibility(View.INVISIBLE);
}
}
private void hideSoftKeyboard(){
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_register:{
Log.d(TAG, "onClick: 尝试注册。");
//检查EditText字段是否为空
if(!isEmpty(mEmail.getText().toString())
&& !isEmpty(mPassword.getText().toString())
&& !isEmpty(mConfirmPassword.getText().toString())){
//检查密码是否匹配
if(doStringsMatch(mPassword.getText().toString(), mConfirmPassword.getText().toString())){
//开始注册任务
registerNewEmail(mEmail.getText().toString(), mPassword.getText().toString());
}else{
Toast.makeText(RegisterActivity.this, "密码不匹配", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(RegisterActivity.this, "您必须填写所有字段", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
}
请注意,我只翻译了您提供的代码部分,不包括问题的回答。
英文:
I am trying to create a register activity to allow a user to register however I keep getting the error:
> Cannot resolve method 'setTimestampsInSnapshotsEnabled' in 'Builder'"
It's only the section which has the setTimestampsInSnapshotsEnabled
that is giving me the issue. Do I need an implementation?
Here is my Java code:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener
{
private static final String TAG = "RegisterActivity";
//widgets
private EditText mEmail, mPassword, mConfirmPassword;
private ProgressBar mProgressBar;
//vars
private FirebaseFirestore mDb;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mEmail = (EditText) findViewById(R.id.input_email);
mPassword = (EditText) findViewById(R.id.input_password);
mConfirmPassword = (EditText) findViewById(R.id.input_confirm_password);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
findViewById(R.id.btn_register).setOnClickListener(this);
mDb = FirebaseFirestore.getInstance();
hideSoftKeyboard();
}
/**
* Register a new email and password to Firebase Authentication
* @param email
* @param password
*/
public void registerNewEmail(final String email, String password){
showDialog();
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
if (task.isSuccessful()){
Log.d(TAG, "onComplete: AuthState: " + FirebaseAuth.getInstance().getCurrentUser().getUid());
//insert some default data
User user = new User();
user.setEmail(email);
user.setUsername(email.substring(0, email.indexOf("@")));
user.setUser_id(FirebaseAuth.getInstance().getUid());
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
mDb.setFirestoreSettings(settings);
FirebaseFirestoreSettings newUserRef = mDb
.collection(getString(R.string.collection_users))
.document(FirebaseAuth.getInstance().getUid());
newUserRef.set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
hideDialog();
if(task.isSuccessful()){
redirectLoginScreen();
}else{
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "Something went wrong.", Snackbar.LENGTH_SHORT).show();
}
}
});
}
else {
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "Something went wrong.", Snackbar.LENGTH_SHORT).show();
hideDialog();
}
// ...
}
});
}
/**
* Redirects the user to the login screen
*/
private void redirectLoginScreen(){
Log.d(TAG, "redirectLoginScreen: redirecting to login screen.");
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void showDialog(){
mProgressBar.setVisibility(View.VISIBLE);
}
private void hideDialog(){
if(mProgressBar.getVisibility() == View.VISIBLE){
mProgressBar.setVisibility(View.INVISIBLE);
}
}
private void hideSoftKeyboard(){
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_register:{
Log.d(TAG, "onClick: attempting to register.");
//check for null valued EditText fields
if(!isEmpty(mEmail.getText().toString())
&& !isEmpty(mPassword.getText().toString())
&& !isEmpty(mConfirmPassword.getText().toString())){
//check if passwords match
if(doStringsMatch(mPassword.getText().toString(), mConfirmPassword.getText().toString())){
//Initiate registration task
registerNewEmail(mEmail.getText().toString(), mPassword.getText().toString());
}else{
Toast.makeText(RegisterActivity.this, "Passwords do not Match", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(RegisterActivity.this, "You must fill out all the fields", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
}
答案1
得分: 1
如果我查看当前 FirebaseFirestoreSettings.Builder
的文档,我看不到 setTimestampsInSnapshotsEnabled
方法。所以这可能解释了错误消息:你试图调用一个不存在的方法。
我猜测你的代码可能是针对较旧版本的 SDK 编写的,当时该方法是存在的。由于现在它已经不存在了,你要么必须使用代码所针对的 SDK 版本,要么移除这个调用。
更新:看起来 timestampsInSnapshotsEnabled
在 Android SDK for Firestore 的版本 22 中被移除了,这是在 2020 年 10 月。
英文:
If I look at the current documentation for FirebaseFirestoreSettings.Builder
, I don't see any setTimestampsInSnapshotsEnabled
method. So that probably explains the error message: you're trying to call a method that doesn't exist.
My guess is that your code was meant for an older version of the SDK when the method did exist. Since it doesn't exist anymore now, you either have to use the version of the SDK that the code was made for, or remove the call.
Update: it looks like timestampsInSnapshotsEnabled
was removed in version 22 of the Android SDK for Firestore back in October 2020.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论