以下代码适用于4个设备中的3个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
表示:当用户将设备旋转到横向模式时,标题栏将被删除,应用程序将全屏显示。到目前为止,除了我的测试中的一个设备之外,它几乎适用于所有设备。
它不起作用的是具有4.1固件的Galaxy S2。 它只是通过将设备从纵向旋转到横向而崩溃。 在横向模式下启动应用程序时,我也可以旋转到纵向。 但是一旦旋转回景观就会崩溃......
导致崩溃的行:
requestWindowFeature(Window.FEATURE_NO_TITLE);
logcat的:
03-20 09:56:08.193: D/AndroidRuntime(8847): Shutting down VM
03-20 09:56:08.193: W/dalvikvm(8847): threadid=1: thread exiting with uncaught exception (group=0x4192d2a0)
03-20 09:56:08.193: E/AndroidRuntime(8847): FATAL EXCEPTION: main
03-20 09:56:08.193: E/AndroidRuntime(8847): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.example/de.example.MainActivity}: java.lang.NullPointerException
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3576)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.ActivityThread.access$800(ActivityThread.java:140)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.os.Looper.loop(Looper.java:137)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.ActivityThread.main(ActivityThread.java:4921)
03-20 09:56:08.193: E/AndroidRuntime(8847): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 09:56:08.193: E/AndroidRuntime(8847): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 09:56:08.193: E/AndroidRuntime(8847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-20 09:56:08.193: E/AndroidRuntime(8847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-20 09:56:08.193: E/AndroidRuntime(8847): at dalvik.system.NativeStart.main(Native Method)
03-20 09:56:08.193: E/AndroidRuntime(8847): Caused by: java.lang.NullPointerException
03-20 09:56:08.193: E/AndroidRuntime(8847): at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.onRestoreInstanceState(PhoneWindow.java:3741)
03-20 09:56:08.193: E/AndroidRuntime(8847): at com.android.internal.policy.impl.PhoneWindow.restorePanelState(PhoneWindow.java:1890)
03-20 09:56:08.193: E/AndroidRuntime(8847): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1841)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.Activity.onRestoreInstanceState(Activity.java:972)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.Activity.performRestoreInstanceState(Activity.java:944)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1145)
03-20 09:56:08.193: E/AndroidRuntime(8847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
03-20 09:56:08.193: E/AndroidRuntime(8847): ... 12 more
不仅应用程序崩溃,整个设备都挂起了。重新激活的唯一方法是通过按住电源按钮几秒钟并重新启动来硬关闭。
有没有人知道为什么会发生这种情况以及如何解决?
我尝试用try / catch包围这些行但没有成功......
祝你好运, 尔根
编辑:
我在MainActivity.java中使用以下代码创建了一个非常简单的新android项目:
package de.example;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
try {
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
} catch (RuntimeException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}
}
布局activity_main只是一个空的RelativeLayout文件。
设备仍然崩溃。如果我删除第一个条件" if(getResources()。getConfiguration()。orientation == Configuration.ORIENTATION_LANDSCAPE)"标题栏在纵向和横向模式下都隐藏,旋转设备时不会崩溃。但看起来我只能在横向模式中摆脱标题栏... 4.1.2中的BUG?有人可以通过4.1.2设备验证吗?
谢谢!