android.util.AndroidRuntimeException:requestFeature()必须在添加内容之前调用 [英] android.util.AndroidRuntimeException: requestFeature() must be called before adding content

查看:320
本文介绍了android.util.AndroidRuntimeException:requestFeature()必须在添加内容之前调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此 android.util.AndroidRuntimeException:requestFeature()添加内容错误之前,必须调用。正如你可以在下面的code时, requestWindowFeature(Window.FEATURE_NO_TITLE)见; 行到来之前的setContentView(R.layout.mainmenu) ; 的code线。这的onCreate()code是在我的活动,几乎每个人相同的格式,我从来没有遇到了麻烦与之前一直到现在。自从我更新到ADT 22很多随机误差已遍地开花。我已经经历了很多的错误,除草,这是我最新的一个。

我能做些什么来解决这个问题?

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.mainmenu);
 

LogCat中

  4月5日至31日:20:43.121:E / AndroidRuntime(14559):致命异常:主要
四月五号至31日:20:43.121:E / AndroidRuntime(14559):java.lang.RuntimeException的:无法启动的活动ComponentInfo {matt.lyons.bibletrivia.lite / matt.lyons.bibletrivia.lite.MainMenu}:android.util .AndroidRuntimeException:requestFeature()必须在添加内容之前调用
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.os.Handler.dispatchMessage(Handler.java:99)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.os.Looper.loop(Looper.java:137)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.ActivityThread.main(ActivityThread.java:5041)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在java.lang.reflect.Method.invokeNative(本机方法)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在java.lang.reflect.Method.invoke(Method.java:511)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在dalvik.system.NativeStart.main(本机方法)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):android.util.AndroidRuntimeException:产生的原因requestFeature()必须添加内容之前调用
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.Activity.requestWindowFeature(Activity.java:3244)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在matt.lyons.bibletrivia.lite.MainMenu.onCreate(MainMenu.java:28)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.Activity.performCreate(Activity.java:5104)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
四月五号至31日:20:43.121:E / AndroidRuntime(14559):11 ...更多
 

解决方案

我也面临这个问题,但是当我打电话窗口请求调用之前的 super.onCreate(),然后问题解决了,请试试吧也很喜欢。

  @覆盖
公共无效的onCreate(包savedInstanceState){
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.mainmenu);
}
 

希望这会帮助你...:)


编辑:有关Android'd新版本的其他可能的解决方案

隐藏在Android 4.0下的状态栏

 <应用
    ...
    机器人:主题=@安卓风格/ Theme.Holo.NoActionBar.Fullscreen>
    ...
< /用途>
 

使用的是活动的主题的优点如下:

  • 它更容易维护,更不容易出错比编程设置一个标志。
  • 这会产生更平滑的用户界面的过渡,因为系统中的信息,它需要实例化你的应用程序的主要活动之前,使您的用户界面。

Android版本低于杰利贝恩

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    //如果Android的版本比杰利贝恩低,使用这个调用来隐藏
    //状态栏。
    如果(Build.VERSION.SDK_INT< 16){
        getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    的setContentView(R.layout.activity_main);
}
 


隐藏在Android 4.1及更高状态栏

 查看decorView = getWindow()getDecorView()。
//隐藏状态栏。
INT uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
//记住,你永远不应该显示操作栏,如果
//状态栏是隐藏的,所以隐藏太多,​​如果有必要的。
动作条动作条= getActionBar();
actionBar.hide();
 

请注意以下内容:

  • 一旦UI标志已被清除(例如,通过导航远离活动),您的应用程序需要的,如果你想再次隐藏棒将它们重置。请参见响应UI能见度更改对于如何监听用户界面的可视性变化讨论,让您的应用程序可以做出相应的反应。
  • 您设置用户界面的标志有差别。如果隐藏在您的活动的onCreate()方法,该系统的酒吧和用户presses首页时,系统栏会重新出现。当用户重新打开活动的onCreate()将不被调用,因此,系统将杆保持可见。如果你想系统用户界面的变化要坚持为用户导航进出你的活动,在onResume()或onWindowFocusChanged()。设置界面标志
  • 的方法setSystemUiVisibility()仅在其如果从调用它的观点是可见的。
  • 从视图导航离开会导致设置setSystemUiVisibility标志()被清除。

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

解决方案

I also faced this problem but when i call window request before calling super.onCreate() then problem was solved, please try it also like..

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

Hope this will help you...:)


Edited: For other possible solutions for Android'd new versions

Hide the Status Bar on Android 4.0 and Lower

<application
    ...
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    ...
</application>

The advantages of using an activity theme are as follows:

  • It's easier to maintain and less error-prone than setting a flag programmatically.
  • It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app's main activity.

Android version is lower than 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);
}


Hide the Status Bar on Android 4.1 and Higher

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();

Note the following:

  • Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.
  • Where you set the UI flags makes a difference. If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
  • The method setSystemUiVisibility() only has an effect if the view you call it from is visible.
  • Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.

这篇关于android.util.AndroidRuntimeException:requestFeature()必须在添加内容之前调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆