ActionBarActivity requestFeature must be called before adding content

Edit reflection matias's comments

Actually, initially I did not have supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); or requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); in my code until i noticed runtime exception when below combinations of actions happened

The user presses the Home button to minimize the application and tried to resume it from Recent applications (which is a long press of the home button)

When the screen rotates (Note: the manifest has no configChange declarations)

Then I thought that showing an indefinite progress bar during initialization should be the cause of the problem, so I only tried calling the request* methods, thinking it would clear it, but nothing happened.

Finally, I removed showPdIndeterminate(); for testing. So nowhere in my code do I show it. However, the same thing happens during the above circumstances.


I have a fragment based on an ActionBarActivity , my layout is wrapped inside a DrawerLayout using two framelayouts to do two scams.

I tried requestFeature () before adding a content error on super.onCreate , but still the same exception for

 @Override protected void onCreate(Bundle savedInstanceState) { Log.e(TAG, "Inside OnCreate"); // supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showPdIndeterminate(); .... } 

and showPdIndeterminate() is

 private void showPdIndeterminate() { pd = ProgressDialog.show(this, "Initializing", "Pls wait..."); pd.setIndeterminate(true); pd.show(); } 

I get a NullPointerException if I try supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); therefore only commented on it.

Error Log:

 06-16 04:04:57.280: D/AndroidRuntime(27280): Shutting down VM 06-16 04:04:57.280: W/dalvikvm(27280): threadid=1: thread exiting with uncaught exception (group=0x413592a0) 06-16 04:04:57.285: E/AndroidRuntime(27280): FATAL EXCEPTION: main 06-16 04:04:57.285: E/AndroidRuntime(27280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.demo.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3553) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.ActivityThread.access$700(ActivityThread.java:140) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.os.Handler.dispatchMessage(Handler.java:99) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.os.Looper.loop(Looper.java:137) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.ActivityThread.main(ActivityThread.java:4898) 06-16 04:04:57.285: E/AndroidRuntime(27280): at java.lang.reflect.Method.invokeNative(Native Method) 06-16 04:04:57.285: E/AndroidRuntime(27280): at java.lang.reflect.Method.invoke(Method.java:511) 06-16 04:04:57.285: E/AndroidRuntime(27280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 06-16 04:04:57.285: E/AndroidRuntime(27280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 06-16 04:04:57.285: E/AndroidRuntime(27280): at dalvik.system.NativeStart.main(Native Method) 06-16 04:04:57.285: E/AndroidRuntime(27280): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 06-16 04:04:57.285: E/AndroidRuntime(27280): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:267) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.Activity.requestWindowFeature(Activity.java:3320) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:63) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 06-16 04:04:57.285: E/AndroidRuntime(27280): at com.example.demo.MainActivity.onCreate(MainActivity.java:464) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.Activity.performCreate(Activity.java:5206) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 06-16 04:04:57.285: E/AndroidRuntime(27280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 06-16 04:04:57.285: E/AndroidRuntime(27280): ... 12 more 

Note: I get this exception when changing orientation, and also when I launch it from the list of recent applications by pressing the home button

This exception ** ultimately **, which occurs regardless of the presence of (not having) setRetainInstance(true); in fragment onActivityCreated() or onCreate () `

Why is this happening? How to solve it?

+8
android android-fragments android-actionbaractivity
source share
3 answers

Override setContentView in your activity and see where / what the method calls. Once you know what is called, Iโ€™m sure that we can find an acceptable solution.

 @Override public void setContentView (int layoutResID) { //Set a break point on the next line or log out a message. super.setContentView(layoutResID); } 
+1
source share

android.support.v7.app.ActionBarActivity modifies the contents of the window by adding an ActionBar . In addition, FEATURE_INDETERMINATE_PROGRESS depends on whether the action bar is present or not.

Try something like this:

 @Override protected void onCreate(Bundle savedInstanceState) { ABD = ActionBarActivityDelegate.createDelegate(this); super.onCreate(savedInstanceState); ABD.onCreate(savedInstanceState); } 

The ActionBarActivityDelegate class can be found here .

Finally, you should take a look at the orientation lock of your screens, if possible. This is described in detail here .

0
source share

Try calling requestWindowFeature() before setcontentView() , but after onCreate() .

It works for me.

0
source share

All Articles