使用Firestore的collection.add()时应用程序崩溃。

7 浏览
0 Comments

使用Firestore的collection.add()时应用程序崩溃。

作为一个Android开发的新手,如果我听起来很琐碎或者说得太长,请原谅我,但我在尝试向Firestore中的集合添加项目时遇到了困难。\n我在某个教程的评论部分读到,这在API 25以下发生(我使用的是24),但我不确定原因是什么。\n以下是我的代码的一部分:\n

FirebaseFirestore db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registro);
    FirebaseFirestore firestore = FirebaseFirestore.getInstance();
    FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
            .setTimestampsInSnapshotsEnabled(true)
            .build();
    firestore.setFirestoreSettings(settings);
    db = FirebaseFirestore.getInstance();
}
public void sendInfo(View view){
        CollectionReference dbArtists = db.collection("artistas");
        Artist a = new Artist("0", "A", 10);
        dbArtists.add(a)
    }

\nsendInfo与一个工作正常的按钮相关联。导致应用崩溃的代码行是\n

dbArtists.add(a)

\n因此,logcat中显示了一个警告:\n

com.esiete.anapptitle W/Firestore: (0.6.6-dev) [Firestore]: The behavior for java.util.Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
    .setTimestampsInSnapshotsEnabled(true)
    .build();
firestore.setFirestoreSettings(settings);
With this change, timestamps stored in Cloud Firestore will be read back as com.google.firebase.Timestamp objects instead of as system java.util.Date objects. So you will also need to update code expecting a java.util.Date to instead expect a Timestamp. For example:
// Old:
java.util.Date date = snapshot.getDate("created_at");
// New:
Timestamp timestamp = snapshot.getTimestamp("created_at");
java.util.Date date = timestamp.toDate();
Please audit all existing usages of java.util.Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.

\n即使添加了以上显示的代码,崩溃仍然存在。logcat显示:\n

8-11-13 21:44:09.290 30030-30030/com.esiete.anapptitle D/AndroidRuntime: Shutting down VM
2018-11-13 21:44:09.294 30030-30030/com.esiete.anapptitle E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.esiete.anapptitle, PID: 30030
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:5612)
        at android.view.View$PerformClick.run(View.java:22285)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6123)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:5612) 
        at android.view.View$PerformClick.run(View.java:22285) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6123) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
     Caused by: java.lang.RuntimeException: No properties to serialize found on class com.esiete.anapptitle.Artist
        at com.google.firebase.firestore.g.zzg$zza.(SourceFile:643)
        at com.google.firebase.firestore.g.zzg.zza(SourceFile:331)
        at com.google.firebase.firestore.g.zzg.zzb(SourceFile:152)
        at com.google.firebase.firestore.g.zzg.zza(SourceFile:1085)
        at com.google.firebase.firestore.UserDataConverter.convertPOJO(SourceFile:421)
        at com.google.firebase.firestore.CollectionReference.add(SourceFile:125)
        at com.esiete.anapptitle.RegistroActivity.enviarRegistro(RegistroActivity.java:82)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:5612) 
        at android.view.View$PerformClick.run(View.java:22285) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6123) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
2018-11-13 21:44:09.298 4088-5186/? W/ActivityManager:   Force finishing activity com.esiete.anapptitle/.RegistroActivity
2018-11-13 21:44:09.306 4088-30868/? W/DropBoxManagerService: Dropping: data_app_crash (1752 > 0 bytes)

\n非常感谢任何帮助或建议。

0
0 Comments

问题原因:在Firebase数据库中,Artist类没有定义与数据库中文档相匹配的公共字段或公共getter和setter方法。

解决方法1:定义一个Java类,其中包含与数据库中文档完全匹配的公共字段。

public class Artist {
    public String artistId;
    public String name;
    public long sales;
}

解决方法2:定义一个Java类,遵循JavaBean命名规范,并包含公共getter和setter方法。

public class Artist {
    String artistId;
    String name;
    long sales;
    public Artist() {} // 空构造函数是必需的
    public Artist(String artistId, String name, long sales) {
        this.artistId = artistId;
        this.name = name;
        this.sales = sales;
    }
    public String getArtistId() { return this.artistId; }
    public void setArtistId(String artistId) { this.artistId = artistId; }
    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }
    public long getRanking() { return this.ranking; }
    public void setRanking(long ranking) { this.ranking = ranking; }
}

这两种解决方法都可以使Firebase客户端正确读取和写入数据库中的值。

感谢你的帮助,问题立即得到解决!我会记住这个方法。我只需稍微修改一下你的答案,最后两个String语句实际上应该是long语句。

0