【问题标题】:How to prevent my android application to collapse when the screen orientation changes in the lock screen?当锁定屏幕中的屏幕方向发生变化时,如何防止我的 android 应用程序崩溃?
【发布时间】:2013-06-11 18:39:39
【问题描述】:

我最近发现了这个问题。 我正在使用带有 Android 4.1 的三星 Tab 7'' 进行测试。

我有一个新的 android 应用程序项目。 这里我们有垃圾.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" /> 
</RelativeLayout>

以及调用它的 Activity:

package com.example.trash;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.trash);
}

}

到目前为止非常简单。然后我把这段代码放在 AndroidManifest 的 MainActivity 选项卡中:

android:screenOrientation="portrait"

当我出现问题时: 1.锁屏 2.将平板电脑的方向更改为横向 3.然后解锁屏幕 4. 令我惊讶的是,我的应用程序没有返回纵向,而是因为一个简单的错误 (Resources$NotFoundException) 而崩溃:

06-15 00:12:37.390: E/AndroidRuntime(6452): java.lang.RuntimeException: Unable to start         activity ComponentInfo{com.example.trash/com.example.trash.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001

我可以做些什么来避免这个问题,而不是为我的应用程序制作横向布局?

【问题讨论】:

    标签: android locking screen orientation collapse


    【解决方案1】:

    你可以试试打电话 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 在你的活动中 onCreate()

    【讨论】:

      【解决方案2】:

      1 您的项目必须面向 3.0 或更高版本;

      2 在您的活动选项卡中(在 android 清单中),您必须添加以下两行:

      android:screenOrientation="portrait" or android:screenOrientation="landscape"
      android:configChanges="orientation|screenSize"
      

      3 在您的 Activity 类中,添加以下方法:

      @Override
      public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
      }   
      

      这样工作。将 configChanges 添加到您的 Activity 选项卡,将使 onConfigurationChanged 方法在每次屏幕在横向和纵向之间切换时被调用,反之亦然。然后,在方法中我们使用 setRequestOrientation 来强制 Activity 保持在某个方向。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多