无法解决方法 ‘setTimestampsInSnapshotsEnabled’ 在 ‘Builder’ 中的问题。

huangapple go评论98阅读模式
英文:

Cannot resolve method 'setTimestampsInSnapshotsEnabled' in 'Builder'

问题

我尝试创建一个注册活动,以允许用户注册,但是我一直收到以下错误消息:

  1. > 'Builder' 中无法解析方法 'setTimestampsInSnapshotsEnabled'

只有包含 setTimestampsInSnapshotsEnabled 的部分出现了问题。我需要进行实现吗?

以下是我的Java代码:

  1. public class RegisterActivity extends AppCompatActivity implements View.OnClickListener
  2. {
  3. private static final String TAG = "RegisterActivity";
  4. //widgets
  5. private EditText mEmail, mPassword, mConfirmPassword;
  6. private ProgressBar mProgressBar;
  7. //vars
  8. private FirebaseFirestore mDb;
  9. @Override
  10. protected void onCreate(@Nullable Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_register);
  13. mEmail = (EditText) findViewById(R.id.input_email);
  14. mPassword = (EditText) findViewById(R.id.input_password);
  15. mConfirmPassword = (EditText) findViewById(R.id.input_confirm_password);
  16. mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
  17. findViewById(R.id.btn_register).setOnClickListener(this);
  18. mDb = FirebaseFirestore.getInstance();
  19. hideSoftKeyboard();
  20. }
  21. /**
  22. * 向Firebase Authentication注册新的电子邮件和密码
  23. * @param email
  24. * @param password
  25. */
  26. public void registerNewEmail(final String email, String password){
  27. showDialog();
  28. FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
  29. .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  30. @Override
  31. public void onComplete(@NonNull Task<AuthResult> task) {
  32. Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
  33. if (task.isSuccessful()){
  34. Log.d(TAG, "onComplete: AuthState: " + FirebaseAuth.getInstance().getCurrentUser().getUid());
  35. //插入一些默认数据
  36. User user = new User();
  37. user.setEmail(email);
  38. user.setUsername(email.substring(0, email.indexOf("@")));
  39. user.setUser_id(FirebaseAuth.getInstance().getUid());
  40. FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
  41. .setTimestampsInSnapshotsEnabled(true)
  42. .build();
  43. mDb.setFirestoreSettings(settings);
  44. FirebaseFirestoreSettings newUserRef = mDb
  45. .collection(getString(R.string.collection_users))
  46. .document(FirebaseAuth.getInstance().getUid());
  47. newUserRef.set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
  48. @Override
  49. public void onComplete(@NonNull Task<Void> task) {
  50. hideDialog();
  51. if(task.isSuccessful()){
  52. redirectLoginScreen();
  53. }else{
  54. View parentLayout = findViewById(android.R.id.content);
  55. Snackbar.make(parentLayout, "出现了问题。", Snackbar.LENGTH_SHORT).show();
  56. }
  57. }
  58. });
  59. }
  60. else {
  61. View parentLayout = findViewById(android.R.id.content);
  62. Snackbar.make(parentLayout, "出现了问题。", Snackbar.LENGTH_SHORT).show();
  63. hideDialog();
  64. }
  65. // ...
  66. }
  67. });
  68. }
  69. /**
  70. * 将用户重定向到登录界面
  71. */
  72. private void redirectLoginScreen(){
  73. Log.d(TAG, "redirectLoginScreen: 重定向到登录界面。");
  74. Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
  75. startActivity(intent);
  76. finish();
  77. }
  78. private void showDialog(){
  79. mProgressBar.setVisibility(View.VISIBLE);
  80. }
  81. private void hideDialog(){
  82. if(mProgressBar.getVisibility() == View.VISIBLE){
  83. mProgressBar.setVisibility(View.INVISIBLE);
  84. }
  85. }
  86. private void hideSoftKeyboard(){
  87. this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  88. }
  89. @Override
  90. public void onClick(View view) {
  91. switch (view.getId()){
  92. case R.id.btn_register:{
  93. Log.d(TAG, "onClick: 尝试注册。");
  94. //检查EditText字段是否为空
  95. if(!isEmpty(mEmail.getText().toString())
  96. && !isEmpty(mPassword.getText().toString())
  97. && !isEmpty(mConfirmPassword.getText().toString())){
  98. //检查密码是否匹配
  99. if(doStringsMatch(mPassword.getText().toString(), mConfirmPassword.getText().toString())){
  100. //开始注册任务
  101. registerNewEmail(mEmail.getText().toString(), mPassword.getText().toString());
  102. }else{
  103. Toast.makeText(RegisterActivity.this, "密码不匹配", Toast.LENGTH_SHORT).show();
  104. }
  105. }else{
  106. Toast.makeText(RegisterActivity.this, "您必须填写所有字段", Toast.LENGTH_SHORT).show();
  107. }
  108. break;
  109. }
  110. }
  111. }
  112. }

请注意,我只翻译了您提供的代码部分,不包括问题的回答。

英文:

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:

  1. public class RegisterActivity extends AppCompatActivity implements View.OnClickListener
  2. {
  3. private static final String TAG = &quot;RegisterActivity&quot;;
  4. //widgets
  5. private EditText mEmail, mPassword, mConfirmPassword;
  6. private ProgressBar mProgressBar;
  7. //vars
  8. private FirebaseFirestore mDb;
  9. @Override
  10. protected void onCreate(@Nullable Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_register);
  13. mEmail = (EditText) findViewById(R.id.input_email);
  14. mPassword = (EditText) findViewById(R.id.input_password);
  15. mConfirmPassword = (EditText) findViewById(R.id.input_confirm_password);
  16. mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
  17. findViewById(R.id.btn_register).setOnClickListener(this);
  18. mDb = FirebaseFirestore.getInstance();
  19. hideSoftKeyboard();
  20. }
  21. /**
  22. * Register a new email and password to Firebase Authentication
  23. * @param email
  24. * @param password
  25. */
  26. public void registerNewEmail(final String email, String password){
  27. showDialog();
  28. FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
  29. .addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
  30. @Override
  31. public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
  32. Log.d(TAG, &quot;createUserWithEmail:onComplete:&quot; + task.isSuccessful());
  33. if (task.isSuccessful()){
  34. Log.d(TAG, &quot;onComplete: AuthState: &quot; + FirebaseAuth.getInstance().getCurrentUser().getUid());
  35. //insert some default data
  36. User user = new User();
  37. user.setEmail(email);
  38. user.setUsername(email.substring(0, email.indexOf(&quot;@&quot;)));
  39. user.setUser_id(FirebaseAuth.getInstance().getUid());
  40. FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
  41. .setTimestampsInSnapshotsEnabled(true)
  42. .build();
  43. mDb.setFirestoreSettings(settings);
  44. FirebaseFirestoreSettings newUserRef = mDb
  45. .collection(getString(R.string.collection_users))
  46. .document(FirebaseAuth.getInstance().getUid());
  47. newUserRef.set(user).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
  48. @Override
  49. public void onComplete(@NonNull Task&lt;Void&gt; task) {
  50. hideDialog();
  51. if(task.isSuccessful()){
  52. redirectLoginScreen();
  53. }else{
  54. View parentLayout = findViewById(android.R.id.content);
  55. Snackbar.make(parentLayout, &quot;Something went wrong.&quot;, Snackbar.LENGTH_SHORT).show();
  56. }
  57. }
  58. });
  59. }
  60. else {
  61. View parentLayout = findViewById(android.R.id.content);
  62. Snackbar.make(parentLayout, &quot;Something went wrong.&quot;, Snackbar.LENGTH_SHORT).show();
  63. hideDialog();
  64. }
  65. // ...
  66. }
  67. });
  68. }
  69. /**
  70. * Redirects the user to the login screen
  71. */
  72. private void redirectLoginScreen(){
  73. Log.d(TAG, &quot;redirectLoginScreen: redirecting to login screen.&quot;);
  74. Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
  75. startActivity(intent);
  76. finish();
  77. }
  78. private void showDialog(){
  79. mProgressBar.setVisibility(View.VISIBLE);
  80. }
  81. private void hideDialog(){
  82. if(mProgressBar.getVisibility() == View.VISIBLE){
  83. mProgressBar.setVisibility(View.INVISIBLE);
  84. }
  85. }
  86. private void hideSoftKeyboard(){
  87. this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  88. }
  89. @Override
  90. public void onClick(View view) {
  91. switch (view.getId()){
  92. case R.id.btn_register:{
  93. Log.d(TAG, &quot;onClick: attempting to register.&quot;);
  94. //check for null valued EditText fields
  95. if(!isEmpty(mEmail.getText().toString())
  96. &amp;&amp; !isEmpty(mPassword.getText().toString())
  97. &amp;&amp; !isEmpty(mConfirmPassword.getText().toString())){
  98. //check if passwords match
  99. if(doStringsMatch(mPassword.getText().toString(), mConfirmPassword.getText().toString())){
  100. //Initiate registration task
  101. registerNewEmail(mEmail.getText().toString(), mPassword.getText().toString());
  102. }else{
  103. Toast.makeText(RegisterActivity.this, &quot;Passwords do not Match&quot;, Toast.LENGTH_SHORT).show();
  104. }
  105. }else{
  106. Toast.makeText(RegisterActivity.this, &quot;You must fill out all the fields&quot;, Toast.LENGTH_SHORT).show();
  107. }
  108. break;
  109. }
  110. }
  111. }
  112. }

答案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.

huangapple
  • 本文由 发表于 2023年2月8日 22:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定