【问题标题】:App crashing while retrieving from Firebase Firestore从 Firebase Firestore 检索时应用程序崩溃
【发布时间】:2021-12-13 05:15:57
【问题描述】:

我尝试制作一个从 Firebase Firestore 获取数据的 RecyclerView,但它崩溃了。

适配器没有错误,因为当我手动将数据添加到 ArrayList 时它工作得很好,我评论了这些行。

我排除了从代码中导入标签,一切都很好。 我还仔细检查了 Firestore 和我的代码中的集合名称,它匹配。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    RecyclerView items;
    ArrayList<Item> itemsList;
    MyAdapter adapter;
    FirebaseFirestore db;
    ProgressDialog pD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pD = new ProgressDialog(this);
        pD.setCancelable(false);
        pD.setMessage("Retrieving Data...");
        pD.show();

        items = findViewById(R.id.recycle);
        items.setHasFixedSize(true);
        items.setLayoutManager(new LinearLayoutManager(this));

        db = FirebaseFirestore.getInstance();
        itemsList = new ArrayList<Item>();
        adapter = new MyAdapter(MainActivity.this, itemsList);

        items.setAdapter(adapter);

        itemsList.add(new Item("Test", "Test"));  //This is test line to see if error is in Adapter.

        EventChangeListener();
    }

    private void EventChangeListener() {

        db.collection("Foods")
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {

                        if (error != null){

                            Toast.makeText(MainActivity.this, "Database Error " + error.getMessage(), Toast.LENGTH_SHORT).show();
                            return;
                        }

                        for (DocumentChange dc : value.getDocumentChanges()){

                            if(dc.getType() == DocumentChange.Type.ADDED){

                                itemsList.add(dc.getDocument().toObject(Item.class)); //When I comment this line it works fine
                                //and if I add items to list manually like shown above it works fine

                            }

                            adapter.notifyDataSetChanged();

                        }
                    }
                });
    }
}

我没有收到任何错误,它只是崩溃了。

编辑 1:

我查看了其他与 Logcat 相关的帖子,每个帖子似乎都放了异常:

2021-10-28 10:50:30.801 3159-3159/com.example.recyclerview E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recyclerview, PID: 3159
    java.lang.RuntimeException: Found two getters or fields with conflicting case sensitivity for property: name
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.addProperty(CustomClassMapper.java:736)
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.<init>(CustomClassMapper.java:640)
        at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(CustomClassMapper.java:377)
        at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:540)
        at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:253)
        at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:100)
        at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:183)
        at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:116)
        at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:161)
        at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:97)
        at com.example.recyclerview.MainActivity$1.onEvent(MainActivity.java:75)
        at com.example.recyclerview.MainActivity$1.onEvent(MainActivity.java:61)
        at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2$com-google-firebase-firestore-Query(Query.java:1133)
        at com.google.firebase.firestore.Query$$ExternalSyntheticLambda2.onEvent(Unknown Source:6)
        at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$com-google-firebase-firestore-core-AsyncEventListener(AsyncEventListener.java:42)
        at com.google.firebase.firestore.core.AsyncEventListener$$ExternalSyntheticLambda0.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8595)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

这里还有 Firebase Firestore 的屏幕截图:

数据是我的第一语言而不是英语,也是占位符。

第 61 行是.addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {

第 75 行是itemsList.add(dc.getDocument().toObject(Item.class));

【问题讨论】:

  • 如果应用程序崩溃,则会将堆栈跟踪写入其 logcat 输出。请找到这个并将完整的错误消息和堆栈跟踪添加到您的问题中。另见stackoverflow.com/questions/23353173/…
  • 请编辑您的问题并添加 Frank van Puffelen 要求的信息,还请添加您的数据库结构作为屏幕截图。
  • 另外,Puf,很抱歉回复晚了,我们之间相差 9 小时,我在凌晨 2 点睡觉前发布了这个问题。

标签: java android firebase google-cloud-firestore android-recyclerview


【解决方案1】:

我找到了解决办法。

首先我没有在 Item 类中创建一个空的构造函数。

package com.example.recyclerview;

import android.widget.ImageView;

public class Item {

    protected String Name;
    protected String Type;
    private int photo;

    public Item(String name, String type) {
        Name = name;
        Type = type;
        this.photo = R.drawable.ic_launcher_background;
    }

    
    //I added this
    public Item(){

    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public int getPhoto() {
        return photo;
    }

    public void setPhoto(int photo) {
        this.photo = photo;
    }
}

行被注释。 我在this Stack 帖子上找到了这个答案。

之后我得到了空的 Cardview 布局,因为我没有将 Firestore 名称与 Item 类中的名称匹配。

希望我也能帮助别人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多