“在添加内容”主详细信息模板之前,必须调用requestFeature()

时间:2013-12-14 21:26:15

标签: android android-fragments

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

当我在平板电脑上尝试应用时,使用双窗格布局,当我更改平板电脑的方向时,它会在标题中出现异常崩溃。 仅当细节片段具有内容时才会发生这种情况。

崩溃在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

我该如何解决?

感谢。

4 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,但是当应用程序在后台被操作系统杀死时发生了。我从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

提交了错误报告

答案 1 :(得分:3)

他们都没有解决我的问题。这就是为什么我想出一个讨厌的解决方法。

android OS在后​​台长时间杀了我的应用后,我的应用崩溃了。

调试时我发现我的应用程序正在崩溃 super.onCreate(savedInstanceState);

但只有在savedInstanceState不是null时才会崩溃。 我不需要任何用户输入来保存和重新填充。因此,如果super.onCreate(null)不是savedInstanceState,我只需拨打null

我的评论就在那条线之前

  /*
            * This may be the wrong workaround!!!
            * It demands more research..
            * But for now, if it solves the problem, then it is a solution :D
            * */
        if(savedInstanceState!=null){
            super.onCreate(null);
        } else {
            super.onCreate(savedInstanceState);
        }

将尝试找到合适的解决方案。

答案 2 :(得分:2)

我刚刚在我的某个应用中看到了这一点。在我的案例中,罪魁祸首是在getActivity().getActionBar()中的一个片段中调用了onCreate()。如果不存在,则会产生为您创建contentView的副作用。

将该行移至onViewCreated()解决了问题

答案 3 :(得分:1)

尝试使用getSupportActionBar();代替getActionBar();