成功构建Gradle后,Android应用在运行时崩溃

时间:2018-12-03 09:10:56

标签: android firebase android-recyclerview nullpointerexception adapter

我是androidfirestore的新手。我已经构建了一个小型应用程序,每当我运行我的应用程序时,即使gradle构建成功,该应用程序也会在运行时崩溃。以下是主要活动的代码,Logcat是更好的理解。

package com.example.make;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import io.opencensus.tags.Tag;

public class MainActivity extends AppCompatActivity {

private static final String Tag = "Firelog";

private List<Users> usersList;
private UsersListAdapter usersListAdapter;

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

    RecyclerView mMainList = findViewById(R.id.main_list);

    usersList  = new ArrayList<>();
    usersListAdapter = new UsersListAdapter(usersList);
    mMainList.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    mMainList.setAdapter(usersListAdapter);


    TextView mName = findViewById(R.id.item_name);
    TextView mStatus = findViewById(R.id.item_status);

    try {

        FirebaseFirestore db = FirebaseFirestore.getInstance();

        db.collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                if (e != null) {

                    Log.d(Tag, "Error : " + e.getMessage(), new NullPointerException());


                    for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

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

                            doc.getDocument().getReference().collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
                                @Override
                                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                                    if (e != null) {
                                        Log.d("", "Error :" + e.getMessage(), new NullPointerException());
                                    }

                                    for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

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

                                            Log.d("USer Name :", doc.getDocument().getId());
                                        }
                                    }
                                }
                            });

                            Users users = doc.getDocument().toObject(Users.class);
                            usersList.add(users);
                            usersListAdapter.notifyDataSetChanged();

                        }

                    }
                }
            }
        });
    } catch (NullPointerException error) {
        mName.setText(error.getMessage());
    }
}

}

这是我得到的错误,

  

12-03 13:59:30.576 26036-26036 / com.example.make E / AndroidRuntime:   致命异常:主要       流程:com.example.make,PID:26036       java.lang.NullPointerException:尝试调用虚拟方法'java.util.List   com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()”   空对象引用           在com.example.make.MainActivity $ 1.onEvent(MainActivity.java:65)           在com.example.make.MainActivity $ 1.onEvent(MainActivity.java:56)           在com.google.firebase.firestore.Query.lambda $ addSnapshotListenerInternal $ 2(com.google.firebase:firebase-firestore @@ 17.1.3:885)           在com.google.firebase.firestore.Query $$ Lambda $ 3.onEvent(com.google.firebase:firebase-firestore @@ 17.1.3)           com.google.firebase.firestore.util.ExecutorEventListener.lambda $ onEvent $ 0(com.google.firebase:firebase-firestore @@ 17.1.3:42)           com.google.firebase.firestore.util.ExecutorEventListener $$ Lambda $ 1.run(com.google.firebase:firebase-firestore @@ 17.1.3)           在android.os.Handler.handleCallback(Handler.java:754)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:163)           在android.app.ActivityThread.main(ActivityThread.java:6383)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

我还放置了try catch块来捕获nullpointer异常,但是它没有用。任何帮助,我们都感激不尽。谢谢。

2 个答案:

答案 0 :(得分:3)

错误指出:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()'

在您的代码中:

for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

queryDocumentSnapshots对象为null(可以通过注释推断出来)。

答案 1 :(得分:0)

public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
    if(queryDocumentSnapshots != null){
       for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
           if (doc.getType() == DocumentChange.Type.ADDED) {
              Log.d("USer Name :", doc.getDocument().getId());
           }
       }
    }
}

该方法有明显的注释,QuerySnapshot可为空,因此您必须添加空检查。

相关问题