Ran
Ran

Reputation: 4157

"requestFeature() must be called before adding content" master detail template

I started a new app with the master detail template.

When I try the app on my tablet, using the two-pane layout, it crashes with the exception in the title when I change orientation of the tablet. This happens only if the detail fragment has content.

The crash is in super.onCreate line, I'm not even calling requestFeature, so it's not even my code:

@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);
    }

 }

Stack trace:

 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

How can I solve it?

Thanks.

Upvotes: 4

Views: 1827

Answers (4)

mmin
mmin

Reputation: 221

Try use getSupportActionBar(); instead of getActionBar();

Upvotes: 1

Bishnu
Bishnu

Reputation: 81

Neither of them solved my case. That's why i come up with a nasty workaround.

My app was crashing when the android OS kill my app after long time in background.

While debugging i find out that my app is crashing on super.onCreate(savedInstanceState);

But it is crashing only when the savedInstanceState is not null. And i need no any user inputs to be saved and repopulated. Hence i just make call to super.onCreate(null) if savedInstanceState is not null.

And my comment goes there before that line

  /*
            * 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);
        }

Will be trying to find the appropriate solution to it.

Upvotes: 3

jimi312
jimi312

Reputation: 86

I've just seen this in one of my apps. In my case the culprit was a call to getActivity().getActionBar() in one of my fragments in onCreate(). This has the side effect of creating a contentView for you if one doesn't exist.

Moving that line to onViewCreated() solved the problem

Upvotes: 2

Nejc
Nejc

Reputation: 149

I got the same issue but it happened when the app was killed in the background by the OS. I traced the problem to ActionBarActivityDelegateICS.java from appcompat where the requestWindowFeature() is called after super.onCreate:

    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);
    }

So I imported the appcompat from git and changed the above code to call the super.onCreate() after .requestWindowFeature() which solved the problem. Also submitted a bug report to Google

Upvotes: 4

Related Questions