我收到此android.util.AndroidRuntimeException: requestFeature() must be called before adding content错误。如下面的代码所示,该requestWindowFeature(Window.FEATURE_NO_TITLE);行位于代码行之前setContentView(R.layout.mainmenu);。在我的每一项活动中,此onCreate()代码都是相同的格式,到目前为止,我从没有遇到过麻烦。自从我将ADT更新为22以来,到处都出现了许多随机错误。我已经克服了许多错误,这是我的最新错误。
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.mainmenu)
我该如何解决此错误?
@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
我也遇到了这个问题,但是当我在调用super.onCreate()之前调用窗口请求时,问题就解决了,请尝试一下。
@Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.mainmenu); }
希望对你有帮助…:)
编辑:有关Android新版本的其他可能解决方案
在Android 4.0及更低版本上隐藏状态栏
<application ... android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" > ... </application>
使用活动主题的优点如下:
与以编程方式设置标志相比,它更易于维护并且更不易出错。 这会导致UI过渡更加平滑,因为系统具有实例化应用程序的主要活动之前呈现UI所需的信息。 Android版本低于Jellybean
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // If the Android version is lower than Jellybean, use this call to hide // the status bar. if (Build.VERSION.SDK_INT < 16) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } setContentView(R.layout.activity_main); }
在Android 4.1及更高版本上隐藏状态栏
View decorView = getWindow().getDecorView(); // Hide the status bar. int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); // Remember that you should never show the action bar if the // status bar is hidden, so hide that too if necessary. ActionBar actionBar = getActionBar(); actionBar.hide();
请注意以下几点: