【发布时间】:2020-08-24 13:22:50
【问题描述】:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.darkjp.todo.MyListsFragment.sendSomeToThatRecyclerViewBiatch(MyListsFragment.java:93)
at com.darkjp.todo.MyListsFragment.access$000(MyListsFragment.java:24)
at com.darkjp.todo.MyListsFragment$1.onDataChange(MyListsFragment.java:61)
以及导致崩溃的复选框:
isDoneCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
final DatabaseReference mDone = database.getReference("tasksList/" + taskListIndex).child("task/" + taskIndex);
mDone.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Task updateTask = new Task();
if (snapshot.child("creator").getValue() != null) {
if (snapshot.child("creator").getValue().toString().equals(""))
updateTask.setCreator("no creator found");
updateTask.setCreator(snapshot.child("creator").getValue().toString());
}
if (snapshot.child("title").getValue() != null && !snapshot.child("title").getValue().toString().equals(""))
updateTask.setTitle(snapshot.child("title").getValue().toString());
if (snapshot.child("description").getValue() != null && !snapshot.child("description").getValue().toString().equals(""))
updateTask.setDescription(snapshot.child("description").getValue().toString());
if (snapshot.child("done").getValue() != null)
updateTask.setDone(isDoneCheckBox.isChecked());
mDone.setValue(updateTask);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
});
recyclerView 收到了更改,即 nornam,但 recyclerView 位于我认为“onPause”的片段中。片段已被替换(动态框架布局),我猜应该正在销毁或从后台返回。 onChange 似乎让它崩溃了应用程序。 我正在研究 onPause/onResume 片段行为,以了解如何在 dataOnChange “睡眠”时管理它,但我看不到任何线索。
如果我很好地解决了问题:我尝试找到实际片段中不存在的 recyclerView(并且在 onPause()、OnResume()...时无法访问)。 我不知道为什么要调用该片段(当另一个片段更相关时。我正在考虑的那个片段被 myListFragment 调用/实例化......我想父/子流是那个谜的背后。
【问题讨论】:
-
从
MyListsFragment.java的第93 行开始,找出哪个变量是null以及原因。 -
是的。我做到了。回收商......它是 onPause() 所以,我猜,无法到达并返回一个空值。并且... recyclerView 位于一个片段中,该片段已被另一个片段替换,该片段启动了导致应用程序崩溃的 clickListener。而且...当 myListfragment 处于“活动状态”时,回收器工作正常。
-
我建议您查看 Activity 和 Fragment 生命周期。一定要尊重他们。
-
那是我不明白的。我应该如何处理已启动但 onPause 的片段? null 值是“缺失”片段中元素上的 findViewById。
-
为什么需要暂停片段的视图?
标签: java android android-recyclerview fragment onpause