Android屏幕方向更改导致应用退出 [英] Android Screen Orientation Change Causes App To Quit

查看:121
本文介绍了Android屏幕方向更改导致应用退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以通过设置

I have an app that I set to only display in Portrait mode by setting

android:screenOrientation="portrait"

清单中的每个Activity.这样可以正常工作,并且在旋转手机时我的应用程序屏幕不会旋转.

for each Activity in the manifest. This works fine and my app screen does not rotate when I rotate the phone.

问题是,如果我以纵向手机启动应用程序,则在我的第一个活动运行时(动画启动屏幕),并且在第一个活动仍在运行时,我将手机旋转到横向时,下一个活动启动(从第一个活动线程的finally块启动),应用将退出.它不会强制退出,而只是退出到主屏幕. LogCat中没有抛出任何异常或其他任何事件可以说明发生这种情况的原因.屏幕永远不会旋转(不应该旋转).如果我在横向模式下用手机启动应用程序,则可以正常运行.仅当我从纵向"开始然后旋转到横向"时才会发生.

The problem is, if I start the app with the phone in Portrait orientation, then as my first Activity runs (an animated splash screen) and I rotate the phone to Landscape while the first Activity is still running, when the next activity is launched (it is launched from the finally block of the first activity's thread) the app will quit. It does not force quit, it simply exits to the home screen. There are no exceptions thrown or anything else in LogCat that tells why this is happening. The screen never rotates (as it shouldn't). If I start the app with the phone in Landscape it works fine. This only happens when I start in Portrait and rotate to Landscape after.

我已经尝试过

android:configChanges="orientation"

在每项活动中,我也尝试过

in every activity and I have also tried

android:configChanges="orientation|keyboard|keyboardHidden|screenLayout"

在活动中是否实现onConfigurationChanged的情况下,我尝试了上述方法.当我实现它时,我只是忽略了它(只是调用super.onConfigurationChanged).

I tried the above with and without implementing onConfigurationChanged in the activities. When I did implement it I simply ignored it (just calling super.onConfigurationChanged).

除了在清单中设置活动之外,我还尝试通过编程方式将活动设置为纵向".

I have also tried programatically setting the activity to Portrait orientation in addition to setting it in the manifest.

我也可以通过在打开物理键盘的情况下启动应用程序,然后在汽车坞站中启动应用程序,来在原始的Motorola Droid上实现此功能.该应用程序启动后,启动屏幕以纵向方向启动(即使键盘处于打开状态或手机在汽车坞中),但是一旦启动第二个活动,该应用程序就会退出.

I can also get this to happen on the original Motorola Droid by starting the app with the physical keyboard open and starting the app in the car dock. The app starts, splash screen starts in Portrait orientation (even though the keyboard is open or the phone is in car dock) but as soon as that second activity is started the app quits.

我认为这与方向"更改后活动"被销毁没有任何关系.我所有的全局数据都存储在应用程序的自定义Application类中,并且如前所述,在清除全局变量的情况下,我不会遇到任何异常,例如空指针.

I don't think this has anything to do with the Activity being destroyed on an orientation change. All my global data is stored in my app's custom Application class and as I mentioned before, I get no exceptions such as a null pointer in the case of a global getting wiped out.

我正在使用Android 2.3.2在Verizon上运行摩托罗拉Droid原始版本.

I am running a Motorola Droid original on Verizon with Android 2.3.2.

推荐答案

由于方向更改,应用程序关闭,因为您必须在res文件夹中提供特定的资源

The application closes on orientation change because you must provide with particular resources in the res folder

  • 例如:布局土地 在res文件夹中 或
  • Eg:layout-land in res folder or

在运行时通过编程方式处理更改

handle the change at runtime through programmatically

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

这篇关于Android屏幕方向更改导致应用退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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