我最近发现了这个问题。 我正在使用带有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),我的应用程序刚刚崩溃,而不是returnig到纵向方向:
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
我可以做些什么来避免这个问题,而不是为我的应用程序制作横向布局?
答案 0 :(得分:2)
你可以尝试打电话
你的活动中的setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
onCreate()
答案 1 :(得分:0)
android:screenOrientation="portrait" or android:screenOrientation="landscape"
android:configChanges="orientation|screenSize"
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
这就是这样的。将configChanges添加到“活动”选项卡,将使每次屏幕在横向和protrait之间切换时调用onConfigurationChanged方法,反之亦然。然后,在该方法中,我们使用setRequestOrientation强制Activity保持某个方向。