android - 当锁定屏幕中屏幕方向发生变化时,如何防止我的 Android 应用程序崩溃?

标签 android locking screen orientation

我最近发现了这个问题。 我使用运行 Android 4.1 的 Samsung Tab 7'' 进行测试。

我有一个新的 Android 应用程序项目。 这里我们有trash.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

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

最佳答案

你可以尝试打电话 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 在您的 Activity onCreate()

关于android - 当锁定屏幕中屏幕方向发生变化时,如何防止我的 Android 应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17119916/

相关文章:

javascript - Nativescript 使用中继器多键值

java - 如何改变字符串的颜色

c# - 同步访问多个线程可以访问的对象

c - 如何使用 C 在 Windows 中的文件上安装只读锁

android - 如何在android中支持不同的屏幕尺寸

css - 我怎样才能使这个 View 适应所有屏幕尺寸;基于百分比或基于网格

android - Ubuntu - 错误 : Failed to create the SD card

java - NotifyDataSetChanged 在与适配器的 Activity 中

php - mysql查询锁定数据库

android - libgdx 如何集成 admob 插页式 _between_ 游戏屏幕