我有一个 Activity 类,它实现了一个带有两个选项卡的 Actionbar。每个 Tab 都在 TabListener 上调用另一个 Fragment??.class。如果我启动应用程序,则会调用 FragmentXY.class。我可以毫无问题地切换到第二个选项卡。但是,如果我切换回包含其他两个片段作为分屏的第一个选项卡,则应用程序崩溃并抛出错误:InflateException binary xml file line : Error inflating class fragment
tab = actionBar
.newTab()
.setText("My Box")
.setIcon(android.R.drawable.ic_menu_help)
.setTabListener(new MyTabListener<FragmentXY>(this, "myXY",FragmentXY.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("QR-Code")
.setIcon(android.R.drawable.ic_menu_add)
.setTabListener(new MyTabListener<FragmentXYZ>(this, "barcode",FragmentXYZ.class));
actionBar.addTab(tab);
每个 Fragment.class 都调用一个 xml 资源,该资源包含一次其他两个片段或仅一个片段。
片段XY.class
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_tab, container, false);
return view;
}
first_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:name="com.febro.myfragmenttest.ListFragment"
android:id="@+id/listFragment"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.febro.myfragmenttest.ListFragment" ></fragment>
<fragment
android:name="com.febro.myfragmenttest.DetailFragment"
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.febro.myfragmenttest.DetailFragment" >
</fragment>
</LinearLayout>
回调方法有问题吗?
这是来自 logcat 的完整错误报告:
09-12 19:57:06.300: D/AndroidRuntime(2799): Shutting down VM
09-12 19:57:06.300: W/dalvikvm(2799): threadid=1: thread exiting with uncaught exception (group=0x4015d760)
09-12 19:57:06.310: E/AndroidRuntime(2799): FATAL EXCEPTION: main
09-12 19:57:06.310: E/AndroidRuntime(2799): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
09-12 19:57:06.310: E/AndroidRuntime(2799): at com.febro.myfragmenttest.FragmentXY.onCreateView(FragmentXY.java:26)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:776)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1133)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.app.BackStackRecord.run(BackStackRecord.java:628)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1309)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:398)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.os.Handler.handleCallback(Handler.java:587)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.os.Looper.loop(Looper.java:132)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.app.ActivityThread.main(ActivityThread.java:4126)
09-12 19:57:06.310: E/AndroidRuntime(2799): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 19:57:06.310: E/AndroidRuntime(2799): at java.lang.reflect.Method.invoke(Method.java:491)
09-12 19:57:06.310: E/AndroidRuntime(2799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
09-12 19:57:06.310: E/AndroidRuntime(2799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
09-12 19:57:06.310: E/AndroidRuntime(2799): at dalvik.system.NativeStart.main(Native Method)
09-12 19:57:06.310: E/AndroidRuntime(2799): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f090008, tag null, or parent id 0xffffffff with another fragment for com.febro.myfragmenttest.ListFragment
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.app.Activity.onCreateView(Activity.java:4182)
09-12 19:57:06.310: E/AndroidRuntime(2799): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
09-12 19:57:06.310: E/AndroidRuntime(2799): ... 18 more
09-12 19:57:06.320: W/ActivityManager(302): Force finishing activity com.febro.myfragmenttest/.ActionbarMain
09-12 19:57:06.400: D/dalvikvm(302): GC_FOR_ALLOC freed 13157K, 52% free 12567K/26055K, paused 68ms