android - "requestFeature() must be called before adding content"主从模板

标签 android android-fragments

我使用主详细信息模板启动了一个新应用。

当我在我的平板电脑上试用该应用程序时,使用双 Pane 布局,当我改变平板电脑的方向时,它会崩溃,标题中出现异常。 仅当详细信息 fragment 有内容时才会发生这种情况。

崩溃发生在 super.onCreate 行,我什至没有调用 requestFeature,所以它甚至不是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_list);

    if (findViewById(R.id.item_detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-large and
        // res/values-sw600dp). If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;

        // In two-pane mode, list items should be given the
        // 'activated' state when touched.
        ((ItemListFragment) getSupportFragmentManager().findFragmentById(
                R.id.item_list)).setActivateOnItemClick(true);
    }

 }

堆栈跟踪:

 12-14 23:18:44.716: E/AndroidRuntime(32065): FATAL EXCEPTION: main
 12-14 23:18:44.716: E/AndroidRuntime(32065): Process: com.manor.barcam, PID: 32065
 12-14 23:18:44.716: E/AndroidRuntime(32065): java.lang.RuntimeException: Unable to  start activity ComponentInfo{com.manor.barcam/com.manor.barcam.ItemListActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.ActivityThread.access$900(ActivityThread.java:135)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at  android.os.Handler.dispatchMessage(Handler.java:102)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.os.Looper.loop(Looper.java:136)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.ActivityThread.main(ActivityThread.java:5017)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at java.lang.reflect.Method.invokeNative(Native Method)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at java.lang.reflect.Method.invoke(Method.java:515)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at dalvik.system.NativeStart.main(Native Method)
 12-14 23:18:44.716: E/AndroidRuntime(32065): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.Activity.requestWindowFeature(Activity.java:3298)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:63)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at com.manor.barcam.ItemListActivity.onCreate(ItemListActivity.java:54)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.Activity.performCreate(Activity.java:5231)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 12-14 23:18:44.716: E/AndroidRuntime(32065):   ... 12 more

我该如何解决?

谢谢。

最佳答案

我遇到了同样的问题,但它发生在应用程序在后台被操作系统杀死时。我从 appcompat 将问题追溯到 ActionBarActivityDelegateICS.java,其中在 super.onCreate 之后调用了 requestWindowFeature():

    super.onCreate(savedInstanceState);

    if (mHasActionBar) {
        // If action bar is requested by inheriting from the appcompat theme,
        // the system will not know about that. So explicitly request for an action bar.
        mActivity.requestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
    }
    if (mOverlayActionBar) {
        mActivity.requestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY);
    }

所以我从 git 导入了 appcompat并更改上面的代码以在解决问题的 .requestWindowFeature() 之后调用 super.onCreate() 。 还向 Google 提交了错误报告

关于android - "requestFeature() must be called before adding content"主从模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588438/

相关文章:

Android Retrofit如何从api获取图片

android - 未显示主要 Activity 的 onCreate 中的进度对话框

java - onClickListener 无法在 Fragment 中工作

android - 最佳实践 : Navigation Drawer

android - 默认不打开抽屉导航

android - 无法使用 Android Studio 4.2 将 Gradle 与 Firebase Crashlytic 2.6.0 同步

android - 在 API 26 之前从 TypedArray 获取字体资源

android - 可以在 ListActivity 中使用 ListPreference 模态视图吗?

android - 自定义 Fragment 无法转换为其他自定义 Fragment

java - Android 错误 : type mismatch cannot convert from android. app.actionbar 到 android.support.v7.app.actionbar