我对下面的代码有些困惑,我在添加N
之前先在Fragment
,Fragment
方法内添加onCreateView()
。从我的角度来看,因为我尚未设置View
,所以当View
调用以获取该R.id.rv_container
的{{1}}时,它将获取FrameLayout
指针异常,但是没有发生。运行良好。
那是什么原因呢?为什么我对Child Fragment
指针异常错误?
片段类:
null
片段布局(R.layout.fragment_chat):
null
如果您查看代码
public class Chats extends Fragment { @Override public View onCreateView( @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) { initRvView(); return inflater.inflate(R.layout.fragment_chat, container, false); } private void initRvView() { final String POST_RV = "chat_recycler"; RecyclerViewFragment recyclerViewFragment = (RecyclerViewFragment) getChildFragmentManager() .findFragmentByTag(POST_RV); if (recyclerViewFragment == null) { recyclerViewFragment = new RecyclerViewFragment(FROM_MESSAGING); } getChildFragmentManager().beginTransaction() .replace(R.id.rv_container, recyclerViewFragment, POST_RV) .commit(); } }
在设置<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <FrameLayout android:id="@+id/rv_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
之前正在调用,但未显示任何initRvView()
指针异常。