8

Edit reflecting matias's comments

Actually, originally I had no supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); or requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); in my code until I noticed the runtime exception when below combinations of actions happened

User presses Home button to minimize the application and tried to resume it from Recent Apps (which is long press of the home button)

When Screen rotation occurs (Note: The manifest does not have configChange declarations)

Then i thought showing indeterminate progress bar during initialization should be causing the issue, so only i tried calling request* methods , thinking it will clear it off, but nothing happened..

Finally i removed showPdIndeterminate(); for the sake of testing. Hence nowhere in my code i am showing it. Still the same happens during the aforementioned circumstances


I have a fragment based ActionBarActivity, my layout is wrapped inside DrawerLayout with two framelayouts to hold two frgaments.

I tried requestFeature() must be called before adding content error on super.onCreate but still 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 am getting NullPointerException if I try supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);, so only commented it.

The error log is:

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 am getting this exception on orientation change as well as when i launch it from recent applications list by pressing home button

This exception is **eventually** arising regrdless of having (not having) setRetainInstance(true); in fragment's onActivityCreated() oronCreate()`

Why is this happening? How to solve it?

Community
  • 1
  • 1
nmxprime
  • 1,506
  • 3
  • 25
  • 52
  • possible duplicate of [No FEATURE\_INDETERMINATE\_PROGRESS in ActionBarCompat for Android 2.3-](http://stackoverflow.com/questions/18027603/no-feature-indeterminate-progress-in-actionbarcompat-for-android-2-3) – ianhanniballake Jun 16 '14 at 05:15
  • Per the duplicate: use [ActionBarActivity.supportRequestWindowFeature](http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportRequestWindowFeature(int)) – ianhanniballake Jun 16 '14 at 05:15
  • @Rod_Algonquin, posted error log – nmxprime Jun 16 '14 at 05:38
  • @ianhanniballake, if it's duplicate as you mentioned, i don't find the solution from the suggested link, ca you post an answer from that link – nmxprime Jun 16 '14 at 05:39
  • @nmxprime is it going good without Window.FEATURE_INDETERMINATE_PROGRESS? – berserk Jun 16 '14 at 06:02
  • @nmxprime for the first error type, are you willing to lock your app's orientation (horizontal or vertical)? – SeahawksRdaBest Jul 09 '14 at 04:47
  • 1
    @nmxprime are you using android.support.v7.app.ActionBarActivity? – SeahawksRdaBest Jul 09 '14 at 04:52
  • @SeahawksRdaBest, I am using ` android.support.v7.app.ActionBarActivity`. I am not about to lock the orienttion – nmxprime Jul 09 '14 at 05:08
  • @nmxprime Exactly why are you trying to call `requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)`? That is necessary only if you intend to use `setProgressBarIndeterminateVisibility()`. You do not need it if you just need to show a `ProgressDialog`. – matiash Jul 12 '14 at 19:49
  • @matiash yes I want to show an indeterminate progress bar – nmxprime Jul 13 '14 at 01:11
  • @nmxprime I don't understand. One thing is showing an indeterminate `ProgressDialog` (as you're doing here in `showPdIndeterminate()`. That does **not** require calling `requestWindowFeature()` at all. Another, completely different thing, is using `Activity.setProgressBarIndeterminateVisibility()` (which you're not doing). If this code you pasted is what you intend t do, you can remove `requestWindowFeature()` and it should work fine. – matiash Jul 13 '14 at 03:34
  • @matiash, Actually i had the issue when i was not calling any of the `request*` methods; on getting this error only i tried to call `those request*` methods. I will try your suggestions and update you soon – nmxprime Jul 14 '14 at 04:15
  • @matiash, using `supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);` and `setProgressBarIndeterminateVisibility(true)` together not working, same error. Also calling either independently also esulted in same exception – nmxprime Jul 14 '14 at 04:28
  • @matiash, Please see the edits – nmxprime Jul 14 '14 at 04:29
  • @nmxprime What I meant was: call _none_ of them. They are not needed in this case. – matiash Jul 14 '14 at 04:38
  • @matiash, the same for not calling case also... – nmxprime Jul 14 '14 at 04:39
  • @nmxprime Hello I have same probem with screen orietation did you solve it? – Sasha Dec 11 '14 at 03:25
  • @Sultan, At first the solution was removing progress dialog. But that seemed not to be the solution, rather just a local fix. NOwadays I am using similar situations, where the problem is not occuring. It may possibly be support library bug. Try updating your support library. If using `ActionBarActivitym FragmentActivity, etc.,` just ensure the same happens with `Activity` ! – nmxprime Dec 11 '14 at 04:39
  • but I dont use any progress dialog in my case and I have latest supp. library can you look throw http://stackoverflow.com/questions/27404146/when-orientation-changed-i-got-requestfeature-must-be-called-before-adding-con – Sasha Dec 14 '14 at 15:23

3 Answers3

1

Override setContentView in your Activity and see where / what is calling the method. Once you find out what is calling it, I'm sure we can figure out a workable solution.

@Override
public void setContentView (int layoutResID)
{
  //Set a break point on the next line or log out a message. 
  super.setContentView(layoutResID);
}
Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • Can you suggest any way how to override it, because i don't know what else do there? – nmxprime Jul 16 '14 at 03:48
  • I suspect the problem is that the author may be calling setContentView in an unexpected way (or has something else calling it). If that is this case, the answer is him simply finding that out (which is why I proposed my solution). – Justin Breitfeller Jul 16 '14 at 16:56
  • Justin, if you read my comment again, `what else` it would imply `what else to do other than calling it's super method...` – nmxprime Jul 17 '14 at 04:10
  • If you read the comment I left in the example, you should set a breakpoint there. Once you do that, you can look at the stack trace of the call and see who is calling setContentView before your requestWindowFeature call – Justin Breitfeller Jul 17 '14 at 16:30
  • Thanks Justin let me try it – nmxprime Jul 17 '14 at 16:43
0

The android.support.v7.app.ActionBarActivity changes a window's content by adding an ActionBar. Furthermore the FEATURE_INDETERMINATE_PROGRESS depends on whether an 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.

Lastly, you should look into locking your screens orientation if possible. This is described in detail here.

Community
  • 1
  • 1
SeahawksRdaBest
  • 868
  • 5
  • 17
  • You're referring ActionBarDelegae, which is not in the `android.support.v7` library I am using (I am sure not using out-dated library) – nmxprime Jul 09 '14 at 05:16
  • Actually The app have to support orientaton change – nmxprime Jul 09 '14 at 05:17
  • @nmxprime did you look at the link I provided for the ActionBarActivityDelegate?? its part of suppourt v7 for sure – SeahawksRdaBest Jul 09 '14 at 05:18
  • its available as of 4.4.2_r1. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/support/v7/app/ActionBarActivityDelegate.java/ – SeahawksRdaBest Jul 09 '14 at 05:27
  • and i am getting it reported in android 4.1.2 – nmxprime Jul 09 '14 at 05:37
  • Yes it is available in 4.1.2. Did you try the above code? – SeahawksRdaBest Jul 09 '14 at 05:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56991/discussion-between-nmxprime-and-seahawksrdabest). – nmxprime Jul 09 '14 at 05:42
  • @nmxprime did you add the new library to your project? – SeahawksRdaBest Jul 09 '14 at 07:10
  • I have no doubt that i am using latest library, and this `android.support.v7.app` contains no `ActionBarDelegate` to use. If your support library has this, can you send it to me? – nmxprime Jul 10 '14 at 06:14
  • have you added it as a support library TO YOUR PROJRCT?(different then just adding just the .jar file) .Please see this http://stackoverflow.com/questions/17890589/issue-with-actionbaractivitydelegate-class-app-doesnt-run – SeahawksRdaBest Jul 10 '14 at 10:36
  • I am using the Templates which adds it automatically, do you recommend manually as listed in Android's develper's site? – nmxprime Jul 10 '14 at 12:30
  • Yes. Just having the library doesn't mean your project knows it has to use it. Follow the steps on the android website to add a reference library to your project. – SeahawksRdaBest Jul 10 '14 at 13:37
  • `ActionBarActivityDelegate` is not public. – matiash Jul 12 '14 at 19:39
  • can you help in my case this is not worked for me http://stackoverflow.com/questions/27404146/when-orientation-changed-i-got-requestfeature-must-be-called-before-adding-con – Sasha Dec 10 '14 at 15:07
0

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

Works for me.

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Rujul1993
  • 1,631
  • 2
  • 10
  • 10