无法启动活动ComponentInfo且未向recyclerview显示数据



我正试图从我的Firestore数据库获取数据,我得到了这个错误:

2021-10-12 19:01:18.585 18859-18859/com.dharmik953.mynotes_firebase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dharmik953.mynotes_firebase, PID: 18859
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dharmik953.mynotes_firebase/com.dharmik953.mynotes_firebase.HomePage}: 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:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
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.dharmik953.mynotes_firebase.HomePage.onCreate(HomePage.java:66)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

我认为我的任何布局或其他东西的一个问题,我试图在其他网站找到解决方案,但没有改变错误保持不变。我非常渴望我的firestore数据库学习阶段,这是我的第一个项目,我把我的项目推送到git hub并分享了它的链接。请帮我解决这个问题。

firestore数据库:-

"notes"uid→"myNotes"noteid→"title","content">

Github链接:

https://github.com/dharmik953/MyNotes-firebase

{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
binding = ActivityHomePageBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
datalist=new ArrayList<>();
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager2 = new LinearLayoutManager(getApplicationContext());
//        layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager2);
recyclerView.setAdapter(adapter);
firestore.collection("notes").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<DocumentSnapshot> list=queryDocumentSnapshots.getDocuments();
for(DocumentSnapshot d:list)
{
firebaseModel obj=d.toObject(firebaseModel.class);
datalist.add(obj);
}
adapter.notifyDataSetChanged();
}
});
firestore = FirebaseFirestore.getInstance();
addNotes = findViewById(R.id.add_button);
Objects.requireNonNull(getSupportActionBar()).setTitle("Notes");
addNotes.setOnClickListener(v -> startActivity(new Intent(HomePage.this,addNotes.class)));
}

您将得到以下错误:

由:java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)'引起

因为您试图在初始化firestore对象之前使用它。要解决此异常,请移动以下代码行:

firestore = FirebaseFirestore.getInstance();

:

firestore.collection("notes").get().addOnSuccessListener(/* ... /*);

你的错误就会消失。

相关内容

  • 没有找到相关文章

最新更新