我最近发现了这个问题。 我使用运行 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/