英文:
dataOnChange crashes fragment that has been replaced
问题
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'android.view.View android.view.View.findViewById(int)'
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("未找到创建者");
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 接收到了变化,这是正常的,但是 RecyclerView 位于一个片段中,我想是 "onPause"。片段已被替换(动态 FrameLayout),应该正在销毁或从返回堆栈中返回,我猜测这可能会引发问题。
onChange 似乎导致了应用程序崩溃。
我正在研究 onPause/onResume 片段的行为,看看我如何在它 "休眠" 时处理数据的变化,但我找不到线索。
*如果我理解问题正确:我试图查找在当前片段中不存在的 RecyclerView(在 onPause()、onResume()... 中无法访问)。
我无法想象为什么会调用该片段(当另一个片段更合适时)。我正在考虑的片段是由 myListFragment 调用/实例化的... 我猜测这背后隐藏着一个父/子流程的谜团。*
英文:
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)
and the checkbox causing the crash:
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) {
}
});
}
});
The recyclerView received a changed, which is nornam, but the recyclerView is in a fragment I suppose "onPause". Fragment has been replaced (dynamic frameLayout), and should be on its way to destruction or get back from the backstack, I guess.
The onChange seems to get it crashes the app.
I'm studying the onPause/onResume fragments behavior to see how I could manage a dataOnChange when its "sleeping" but I can't see no clue.
If I got the problem well: i try to find a recyclerView that does not exist in the actual fragment (and can't be reached while onPause(), OnResume()...).
I can't figure out why that fragment is called (when another one would have been more pertinent. The one i'm thinking of is called/instantiated by myListFragment... i guess a parent/children flow is behind that mystery.
答案1
得分: 1
好的...
不是一个生命周期。
我之前在使用getView()
来获取“R.id.xxxxxx”的recyclerView
。但是这是错误的,所以它只能启动一次,无法被恢复。我刚刚改为从一个“经典”的方式来获取recyclerView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_my_lists, container, false);
myRecycler = view.findViewById(R.id.myRecycler);
}
我之前是这样写的:
myRecycler = this.getView().findViewById(R.id.myRecycler);
英文:
Ok...
Was not a lifecycle one.
i was doing a getView() to get the "R.id.xxxxxx" recyclerView. And it was wrong so it launched once but could not be resume. I just, now, get the recyclerView from a "classic"
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_my_lists, container, false);
myRecycler = view.findViewById(R.id.myRecycler);
}
i was doing a
myRecycler = this.getView.findViewById(R.id.myRecycler);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论