相关文章推荐
宽容的刺猬  ·  Jquery ...·  4 月前    · 
欢快的消炎药  ·  Spring boot ...·  1 年前    · 

无法启动活动ComponentInfo,也无法向recyclerview显示数据

1 人关注

我正试图从我的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 link:-

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
android
firebase
google-cloud-platform
google-cloud-firestore
Dharmik
Dharmik
发布于 2021-10-12
1 个回答
Alex Mamo
Alex Mamo
发布于 2021-10-12
已采纳
0 人赞同

你得到的是以下错误。

原因是: java.lang.NullPointerException。试图在一个空对象引用上调用虚拟方法'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)'

因为你试图在 firestore 对象被初始化之前使用它。为了解决这个异常,请移动下面这行代码。

firestore = FirebaseFirestore.getInstance();