通过 XML 或以编程方式使用嵌套片段在 4.2/4.3 设备上效果很好。
当 2.3 尝试使用嵌套片段并拥有 AppCompat 的操作栏时,嵌套片段似乎想要访问操作栏,但找不到它......因为它们嵌套在片段中并且不应该有。
我知道在使用 AppCompat 时会遇到一些问题,例如:AppCompat、fragments 和 2.3,我希望这可以同样轻松地解决。
XML 布局:
<fragment
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="my.package.MyFragment"
tools:layout="@layout/my_layout" />
清单.xml:
<application
...
android:theme="@style/Theme.AppCompat.Light">
堆栈跟踪:
E/AndroidRuntime( 6487): java.lang.RuntimeException: Unable to start activity ComponentInfo{your.package.here/your.package.here.SomeActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080014 (your.package.here:id/action_bar_activity_content) for fragment ProfileFragment{406160e0 #0 id=0x7f080014 SomeFragment}
E/AndroidRuntime( 6487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
E/AndroidRuntime( 6487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
E/AndroidRuntime( 6487): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 6487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
E/AndroidRuntime( 6487): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 6487): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 6487): at android.app.ActivityThread.main(ActivityThread.java:3691)
E/AndroidRuntime( 6487): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 6487): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 6487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
E/AndroidRuntime( 6487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
E/AndroidRuntime( 6487): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 6487): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080014 (your.package.here:id/action_bar_activity_content) for fragment SomeFragment{406160e0 #0 id=0x7f080014 SomeFragment}
E/AndroidRuntime( 6487): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
E/AndroidRuntime( 6487): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
E/AndroidRuntime( 6487): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime( 6487): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
E/AndroidRuntime( 6487): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
E/AndroidRuntime( 6487): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
E/AndroidRuntime( 6487): at android.app.Activity.performStart(Activity.java:3833)
E/AndroidRuntime( 6487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
E/AndroidRuntime( 6487): ... 11 more
如何在仍然使用嵌套片段和 AppCompat 操作栏的同时解决此问题?