更新小部件时出现空列表异常

时间:2018-07-13 21:28:35

标签: android firebase-realtime-database

我正在尝试将数据从Firebase传递到小部件,数据显示在调试中,但是每次我将参数传递给小部件时应用程序崩溃,这是错误:

  

java.lang.NullPointerException:尝试调用虚拟方法   空对象上的'java.lang.String java.lang.Object.toString()'   参考于   com.example.amrkamal.textme.MainActivity $ 4.onDataChange(MainActivity.java:224)   在com.google.android.gms.internal.firebase_database.zzfc.zza(未知   资料来源:13)   com.google.android.gms.internal.firebase_database.zzgx.zzdr(未知   资料来源:2)   com.google.android.gms.internal.firebase_database.zzhd.run(未知   来源:71),位于android.os.Handler.handleCallback(Handler.java:790)   android.os.Handler.dispatchMessage(Handler.java:99)位于   android.os.Looper.loop(Looper.java:164)在   android.app.ActivityThread.main(ActivityThread.java:6494)在   java.lang.reflect.Method.invoke(本机方法)位于   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我取消了List<MessageModel> chatModelsArrayList;

这是我的代码,所有这些我都写在onCreate

chatModelsArrayList = new ArrayList<>();
     chatModelsArrayList.clear();
            DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
            databaseReference.child("messages").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        MessageModel usersModels = new MessageModel(
                                snapshot.child("name").getValue().toString(),
                                snapshot.child("text").getValue().toString());
                        chatModelsArrayList.add(usersModels); }
                    updateWidgetScreen(chatModelsArrayList);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
            Toast.makeText(getApplicationContext(), R.string.AddedTowidget, Toast.LENGTH_LONG).show();


        }

然后用它来更新小部件

private void updateWidgetScreen(List<MessageModel> chatModelsList) {
                String users = getString(R.string.messages_list_name);

                for (MessageModel user : chatModelsList) {

                    users = users
                            + user.getName() + ": "
                            + user.getText() + " \n";
                }
                WidgetUpdateService.startWidgetUpdate(this, users);
            }

请问是什么问题?任何解决方案吗?

Debugging Screen

enter image description here

2 个答案:

答案 0 :(得分:0)

我怀疑代码是异步执行的。您是否尝试过添加空检查,如下所示:

  @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    **if (snapshot != null)**
                    **{**
                    MessageModel usersModels = new MessageModel(
                            snapshot.child("name").getValue().toString(),
                            snapshot.child("text").getValue().toString());
                    chatModelsArrayList.add(usersModels); }
                updateWidgetScreen(chatModelsArrayList);
                    **}**
            }

答案 1 :(得分:0)

要解决此问题,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference messagesRef = rootRef.child("messages");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<MessageModel> chatModelsArrayList = new ArrayList<>();

        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            MessageModel messageModel = ds.getValue(MessageModel.class);
            chatModelsArrayList.add(messageModel );
            Log.d("TAG", messageModel.getName() + " / " + messageModel.getText());
        }

        //Call method to update widget
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
messagesRef.addListenerForSingleValueEvent(valueEventListener);

logcat中的输出将是:

Amr Kamal / fsafsfa
Amr Kamal / hii
//And so on