我在運行時從活動中擴充片段。因此,爲了擡高片段的視圖中的片段I類叫:片段 - 獲取視圖調用onCreateView時出現父錯誤
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.confirm_fragment, container);
}
在這一刻,我得到一個原木崩潰:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
但是,如果我修改我的方法:
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.confirm_fragment, null);
}
其中我現在指定我的容器爲null
,它的工作原理。但是我不明白的是,在代碼崩潰時,我在哪裏指定了父視圖?
是的,如果我指定我的容器爲空,就像我在答案中所做的一樣,它會將attachtoroot作爲false來傳遞。 –