Android 成功 gradle 构建后,应用程序在运行时崩溃
Android app crashes at runtime after successful gradle build
我是 android
和 firestore
的新手。我构建了一个小型应用程序,每当我 运行 我的应用程序时,即使 gradle 构建成功,应用程序也会在 运行 时崩溃。下面主要是activity和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:
FATAL EXCEPTION: main
Process: com.example.make, PID: 26036
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List
com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a
null object reference
at com.example.make.MainActivity.onEvent(MainActivity.java:65)
at com.example.make.MainActivity.onEvent(MainActivity.java:56)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal(com.google.firebase:firebase-firestore@@17.1.3:885)
at com.google.firebase.firestore.Query$$Lambda.onEvent(com.google.firebase:firebase-firestore@@17.1.3)
at com.google.firebase.firestore.util.ExecutorEventListener.lambda$onEvent[=17=](com.google.firebase:firebase-firestore@@17.1.3:42)
at com.google.firebase.firestore.util.ExecutorEventListener$$Lambda.run(com.google.firebase:firebase-firestore@@17.1.3)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6383)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
我还放置了 try catch
块来捕获空指针异常,但它没有 work.Any 帮助表示赞赏。谢谢。
错误指出:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference
在您的代码中:
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
queryDocumentSnapshots 对象为空(可以通过注释推断)。
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是可以为nullable的,所以要添加null检查。
我是 android
和 firestore
的新手。我构建了一个小型应用程序,每当我 运行 我的应用程序时,即使 gradle 构建成功,应用程序也会在 运行 时崩溃。下面主要是activity和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: FATAL EXCEPTION: main Process: com.example.make, PID: 26036 java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference at com.example.make.MainActivity.onEvent(MainActivity.java:65) at com.example.make.MainActivity.onEvent(MainActivity.java:56) at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal(com.google.firebase:firebase-firestore@@17.1.3:885) at com.google.firebase.firestore.Query$$Lambda.onEvent(com.google.firebase:firebase-firestore@@17.1.3) at com.google.firebase.firestore.util.ExecutorEventListener.lambda$onEvent[=17=](com.google.firebase:firebase-firestore@@17.1.3:42) at com.google.firebase.firestore.util.ExecutorEventListener$$Lambda.run(com.google.firebase:firebase-firestore@@17.1.3) at android.os.Handler.handleCallback(Handler.java:754) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6383) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
我还放置了 try catch
块来捕获空指针异常,但它没有 work.Any 帮助表示赞赏。谢谢。
错误指出:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference
在您的代码中:
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
queryDocumentSnapshots 对象为空(可以通过注释推断)。
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是可以为nullable的,所以要添加null检查。