当前位置: 代码迷 >> Android >> 彻底逆天了,这样的代码,报这样的异常
  详细解决方案

彻底逆天了,这样的代码,报这样的异常

热度:74   发布时间:2016-04-28 02:09:25.0
彻底逆天了,这样的代码,报这样的错误!


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}



这样的代码,报这样的错误:

02-02 14:29:22.180: E/AndroidRuntime(15289): FATAL EXCEPTION: main

02-02 14:29:22.180: E/AndroidRuntime(15289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appcamera/com.example.appcamera.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

这个顺序不应该有错吧,怎么办?

------解决思路----------------------
初步鉴定,全屏代码没问题。。。。。
------解决思路----------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
}
------解决思路----------------------
代码复制过来,我这里跑没问题。。。是不是其他的因素影响了
------解决思路----------------------
或许你该clean,手动把机器上的apk卸载了再装
------解决思路----------------------
没啊,我就是主界面,没问题。。。没跳,不跳也可以。。。。
------解决思路----------------------
requestWindowFeature(Window.FEATURE_NO_TITLE);放倒Super.onCreate前面
------解决思路----------------------
引用
02 14:29:22.180: E/AndroidRuntime(15289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appcamera/com.example.appcamera.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content


错误信息提示 让你把requestWindowFeature方法放在添加Content方法(super.onCreate(savedInstanceState))的前面
------解决思路----------------------
检查是不是继承自Activity,如果不是就改下
------解决思路----------------------
估计是sdk版本问题,无标题可以写在androidmanifest.xml中
android:theme="@android:style/Theme.NoTitleBar"  


android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式 
android:theme="@android:style/Theme.NoTitleBar" 不显示应用程序标题栏 
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,并全屏 
android:theme="Theme.Light" 背景为白色 
android:theme="Theme.Light.NoTitleBar" 白色背景并无标题栏 
android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏 
android:theme="Theme.Black" 背景黑色 
android:theme="Theme.Black.NoTitleBar" 黑色背景并无标题栏 
android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏 
android:theme="Theme.Wallpaper" 用系统桌面为应用程序背景 
android:theme="Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏 
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏 
android:theme="Translucent"  透明背景
android:theme="Theme.Translucent.NoTitleBar"  透明背景并无标题
android:theme="Theme.Translucent.NoTitleBar.Fullscreen"  透明背景并无标题,全屏
android:theme="Theme.Panel"   面板风格显示
android:theme="Theme.Light.Panel" 平板风格显示
------解决思路----------------------
引用:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
}


这样试试。
  相关解决方案