我想将onBackPressed移至上一个活动(ViewData.class)时遇到问题,但我不能这样做。有解决问题的解决方案吗?问题是:
Caused by: java.lang.NullPointerException: Provided document path must not be null.
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:906)
at com.google.firebase.firestore.CollectionReference.document(com.google.firebase:firebase-firestore@@21.1.1:103)
at id.MuhammadRafi.StockCount.ViewData.onStart(ViewData.java:200)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
ViewData.java:
@Override
protected void onStart() {
super.onStart();
documentID = getIntent().getStringExtra("documentID");
collectionReference.document(documentID).collection("Products").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
itemsList.clear();
for(DocumentSnapshot documentSnapshot : task.getResult()) {
Items items = documentSnapshot.toObject(Items.class);
itemsList.add(items);
productListAdapter.notifyDataSetChanged();
}
}
}
});
}
MainActivity.java:
public void onBackPressed() {
Intent moveView = new Intent(ScanActivity.this, ViewData.class);
startActivity(moveView);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:2)
如果您想返回上一个活动,则无需
startActivity(moveView);
finish();
在您的onBackPressed()
中。您正在执行的操作是启动该活动的另一个实例,然后将要发送的数据发送回去。
在您的startActivityForResult()
和ViewData.class
的{{1}}中onBackPressed()
MainActivity
然后在您的public void onBackPressed() {
Intent moveView = getIntent();
//Here your can add whatever values you want to send back to your activity
moveView.putExtra("some_key",value)
setResult(Activity.RESULT_OK,moveView);
finish();
}
覆盖ViewData.class
中,您可以在其中检索当前活动发送的数据。参见Getting a Result from an Activity
Firestore的问题是您未使用正确的路径初始化,可能是由于您正在启动onActivityResult()
答案 1 :(得分:0)
NullPointerException:提供的文档路径不能为空
以上错误是由以下行引起的:
documentID = getIntent().getStringExtra("documentID");
此处documentID为空。
要返回上一个活动,只需删除onBackPressed方法或将其重写为:
@Override
public void onBackPressed() {
super.onBackPressed();
}
然后在您当前活动的onCreate()中添加以下行以使向后导航图标起作用-
getSupportActionBar().setDisplayHomeAsUpEnabled(true);