我收到了这个android.util.AndroidRuntimeException: requestFeature() must be called before adding content
错误.正如您在下面的代码中看到的那样,该requestWindowFeature(Window.FEATURE_NO_TITLE);
行在代码行之前setContentView(R.layout.mainmenu);
.这个onCreate()代码在我的每一项活动中都是相同的格式,直到现在我才从未遇到过麻烦.自从我更新到ADT 22以来,到处都出现了很多随机错误.我已经排除了很多这些错误,这是我最新的错误.
我该怎么做才能解决这个错误?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainmenu);
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
vis*_*dra 176
我也遇到了这个问题但是当我在调用super.onCreate()之前调用window请求时问题就解决了,请尝试它也喜欢..
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}
Run Code Online (Sandbox Code Playgroud)
希望对你有帮助...:)
编辑:针对Android新版本的其他可能解决方案
在Android 4.0和更低版本上隐藏状态栏
<application
...
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
Run Code Online (Sandbox Code Playgroud)
使用活动主题的优点如下:
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);
}
Run Code Online (Sandbox Code Playgroud)
在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();
Run Code Online (Sandbox Code Playgroud)
请注意以下事项:
gke*_*kee 12
我android.util.AndroidRuntimeException: requestFeature() must be called before adding content
在使用时得到了异常()
requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
在运行Android 2.3.5(Gingerbread)的旧设备中.我正在使用v7支持库.
当我将其更改为使用时,错误已得到修复:
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
Run Code Online (Sandbox Code Playgroud)
(这是在我的super.onCreate调用修复程序之后).请参阅https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportRequestWindowFeature(int)上的文档
因此,它可能更多是误导性错误消息的情况.
小智 7
如果您将活动用作Dialog(使用Theme.Dialog),请确保扩展Activity而不是ActionBarActivity(使用Android Studio向导为您提供的默认值).然后你可以使用
requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
要么
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)
在super.onCreate()
...之后
归档时间: |
|
查看次数: |
85941 次 |
最近记录: |