[android] java.lang.IllegalArgumentException: Binary XML file line #48: Duplicate id エラーの対処法
目次
java.lang.IllegalArgumentException: Binary XML file line #48: Duplicate id エラーの対処法
Fragmentを入れ子にした。
・MainActivity
・FirstFragment
・SecondFragment(FirstFragmentにAttach)
そのFragmentをintentなどで一旦非表示、再度Fragmentが入れ子になっているFragmentを再表示すると
<java.lang.IllegalArgumentException: Binary XML file line #48: Duplicate id>
というエラーが出る
調べまくってようやく解決。
Duplicateしていたのは
SecondFragmentではなく、FirstFragmentのViewが重複していた様子。
Viewが既にあるのであれば、作成しないという構文を実装した結果、エラーがなくなった。
MainFragment.java
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_main, null, false);
}
SecondFragment fragment = (SecondFragment) this.getChildFragmentManager()
.findFragmentById(R.id.secondFragment);
return view;
}