dataOnChange使已替换的片段崩溃

问题描述

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”。我猜想片段已被替换(动态frameLayout),并且应该正在销毁或从堆栈中退回。 该onChange似乎使它崩溃了。 我正在研究onPause / onResume片段的行为,以了解如何在dataOnChange处于“睡眠”状态时对其进行管理,但看不到任何线索。

如果我很好地解决了这个问题:我尝试找到一个在实际片段中不存在的recyclerView(并且在onPause(),OnResume()时无法访问)。 我无法弄清楚为什么要调用该片段(当另一个片段本来应该更相关时。我想的那个片段是由myListFragment调用/实例化的。。。我想父母/孩子的流在这个谜团后面。 / em>

解决方法

好吧... 这不是生命周期。

我正在执行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);