Caused by: java.lang.IllegalArgumentException: Binary XML file line #39: Duplicate id 0x7f07006d, tag null, or parent id 0x0 with another fragment for com.tvie.ivideo.pad.main.HotVideoFragment
10-28 18:13:47.356: E/AndroidRuntime(8201): FATAL EXCEPTION: main 10-28 18:13:47.356: E/AndroidRuntime(8201): android.view.InflateException: Binary XML file line #39: Error inflating class fragment 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 10-28 18:13:47.356: E/AndroidRuntime(8201): at com.tvie.ivideo.pad.main.MainFragment.onCreateView(MainFragment.java:15) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.os.Handler.handleCallback(Handler.java:730) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.os.Handler.dispatchMessage(Handler.java:92) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.os.Looper.loop(Looper.java:137) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.app.ActivityThread.main(ActivityThread.java:5103) 10-28 18:13:47.356: E/AndroidRuntime(8201): at java.lang.reflect.Method.invokeNative(Native Method) 10-28 18:13:47.356: E/AndroidRuntime(8201): at java.lang.reflect.Method.invoke(Method.java:525) 10-28 18:13:47.356: E/AndroidRuntime(8201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 10-28 18:13:47.356: E/AndroidRuntime(8201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-28 18:13:47.356: E/AndroidRuntime(8201): at dalvik.system.NativeStart.main(Native Method) 10-28 18:13:47.356: E/AndroidRuntime(8201): Caused by: java.lang.IllegalArgumentException: Binary XML file line #39: Duplicate id 0x7f07006d, tag null, or parent id 0x0 with another fragment for com.tvie.ivideo.pad.main.HotVideoFragment 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285) 10-28 18:13:47.356: E/AndroidRuntime(8201): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 10-28 18:13:47.356: E/AndroidRuntime(8201): ... 20 more
解决思路:添加static变量保存onCreateView方法中在第一次创建的view。在以后调用onCreateView方法时,view肯定不是null,那么获取view的parent,将view从parent中删除。
private static View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //View view = inflater.inflate(R.layout.fragment_main, container, false); //return view; if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } try { view = inflater.inflate(R.layout.fragment_main, container, false); } catch (InflateException e) { /* fragment_main is already there, just return view as it is */ } return view; }