android.util.AndroidRuntimeException: requestFeatu

2020-01-23 11:35发布

I am getting this android.util.AndroidRuntimeException: requestFeature() must be called before adding content error. As you can see in the below code, the requestWindowFeature(Window.FEATURE_NO_TITLE); line comes before setContentView(R.layout.mainmenu); line of code. This onCreate() code is the same format in just about every one of my activities and I've never had trouble with it before until now. Ever since I updated to ADT 22 a lot of random errors have been popping up everywhere. I have weeded through a lot of those errors and this is my latest one.

What can I do to fix this error?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mainmenu);

LogCat

05-31 04:20:43.121: E/AndroidRuntime(14559): FATAL EXCEPTION: main
05-31 04:20:43.121: E/AndroidRuntime(14559): java.lang.RuntimeException: Unable to start activity ComponentInfo{matt.lyons.bibletrivia.lite/matt.lyons.bibletrivia.lite.MainMenu}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.os.Looper.loop(Looper.java:137)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at java.lang.reflect.Method.invokeNative(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at java.lang.reflect.Method.invoke(Method.java:511)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at dalvik.system.NativeStart.main(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Activity.requestWindowFeature(Activity.java:3244)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at matt.lyons.bibletrivia.lite.MainMenu.onCreate(MainMenu.java:28)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Activity.performCreate(Activity.java:5104)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-31 04:20:43.121: E/AndroidRuntime(14559):    ... 11 more

标签: java android
11条回答
走好不送
2楼-- · 2020-01-23 12:13

I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to remove the title:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
查看更多
▲ chillily
3楼-- · 2020-01-23 12:14

i think the simplest way to do this is use this below code

  getActionBar().setTitle(null);
查看更多
祖国的老花朵
4楼-- · 2020-01-23 12:20

I also ran into this error from a different workflow. I created a custom DialogFragment class and I created two @Override functions - onCreateView and onCreateDialog. My onCreateView function grabbed a custom layout for the fragment, and my onCreateDialog function created an AlertDialog.Builder.

This appeared to not work because the onCreateDialog is called before onCreateView. After I deleted onCreateView [by moving my custom view inflation into onCreateDialog, i encountered the error:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I realized my difficulty came from trying to implement both overrides since I wanted to 1) use a layout for the main view of the dialog, and 2) use the Builder pre-defined Positive / Negative buttons. My solution was to create the positive / negative buttons in my custom dialog view, so i deleted my implementation of the Override onCreateDialog function.

Hope this helps someone in the future!

Here are some SO questions that helped me:

查看更多
甜甜的少女心
5楼-- · 2020-01-23 12:20

Misplaced your line: super.onCreate(savedInstanceState);

use this Manner:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrscanner);
查看更多
萌系小妹纸
6楼-- · 2020-01-23 12:21

Please try the following options, at least one of these should work for you:

  1. If you are using a DialogFragment then try not overriding both methods onCreateDialog(..) and onCreateView(..) (This change worked for me)

  2. try supportRequestWindowFeature(Window.FEATURE_NO_TITLE)/requestWindowFeature(Window.FEATURE_NO_TITLE) before setContentView(..) in the activity and after super.onCreate(..);

  3. Try option 2 before super.onCreate(..);

查看更多
你好瞎i
7楼-- · 2020-01-23 12:23

I got that exception (android.util.AndroidRuntimeException: requestFeature() must be called before adding content) when using

requestWindowFeature(Window.FEATURE_NO_TITLE);

in an older device running Android 2.3.5 (Gingerbread). I am using the v7 support library.

The error was fixed when I changed it to use:

supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

(This comes after my super.onCreate call in the fix, too). See docs at https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportRequestWindowFeature(int)

So it may be more a case of a misleading error message than anything else.

查看更多
登录 后发表回答