我正在使用FireStore / Firebase处理一本烹饪书应用程序而且我一直得到以下引用是空指针异常,我有点迷失在我如何解决这个问题上,有人可能有所帮助吗?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dmange.kitchen, PID: 25648
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dmange.kitchen/com.example.dmange.kitchen.ShowDetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference
at com.example.dmange.kitchen.ShowDetailsActivity.loadRecipeList(ShowDetailsActivity.java:113)
at com.example.dmange.kitchen.ShowDetailsActivity.onCreate(ShowDetailsActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的ShowDetailsActiviy
中的代码参考:https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection
public class ShowDetailsActivity extends AppCompatActivity {
private ProgressDialog progressDialog;
private FirebaseFirestore firestoreDB;
private ListenerRegistration firestoreListener;
private static final String TAG = "MainActivity";
RecyclerView recyclerView;
MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
//recyclerView.setHasFixedSize(true);
//recyclerView.setLayoutManager(new LinearLayoutManager(ShowDetailsActivity.this));
progressDialog = new ProgressDialog(ShowDetailsActivity.this);
progressDialog.setMessage("Data Loading");
progressDialog.show();
// databaseReference = FirebaseDatabase.getInstance().getReference(MainActivity.Database_Path);
loadRecipeList();
firestoreDB = FirebaseFirestore.getInstance();
firestoreListener = firestoreDB.collection("Recipes")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (e != null){
Log.e(TAG,"Listen failed!",e );
return;
}
List<Recipe> recipeList = new ArrayList<>();
for (DocumentSnapshot doc : queryDocumentSnapshots) {
Recipe recipe = doc.toObject(Recipe.class);
recipe.setId(doc.getId());
recipeList.add(recipe);
}
mAdapter = new MyAdapter(recipeList, getApplicationContext(), firestoreDB);
recyclerView.setAdapter(mAdapter);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
firestoreListener.remove();
}
private void loadRecipeList() {
firestoreDB.collection("Recipes")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
List<Recipe> recipeList = new ArrayList<>();
for (QueryDocumentSnapshot doc : task.getResult()) {
Log.d(TAG, doc.getId() + " => " + doc.getData());
//HashMap<String, Object> result = task.getResult().getData();
Recipe recipe = doc.toObject(Recipe.class);
/* recipe.setId(doc.getId());
recipeList.add(recipe);*/
Log.d(TAG, recipe.getId() + " => " + recipe.getTitle());
recipeList.add(recipe);
}
mAdapter = new MyAdapter(recipeList,getApplicationContext(), firestoreDB);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
}
这是我的模型食谱
package com.example.dmange.kitchen;
import java.util.HashMap;
import java.util.Map;
/**
* Created by dmange on 2018-04-08.
*/
public class Recipe {
private String title;
private String recipe;
private String Id;
public Recipe(){}
public Recipe(String id,String title, String recipe ){
this.Id = id;
this.title = title;
this.recipe = recipe;
}
public void setId(String id) {
Id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setRecipe(String recipe) {
this.recipe = recipe;
}
public String getId() {
return Id;
}
public String getTitle() {
return title;
}
public String getRecipe() {
return recipe;
}
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("Title", this.title);
result.put("Recipe", this.recipe);
return result;
}
}