横向模式下的应用程序崩溃

App Crashes in landscape mode

应用程序在 tablet/mobile 方向更改后重新启动应用程序时崩溃,

我打算只在横向模式下开发一个应用程序,我在 layout-land 中做了一个名为 acitivty_main 的布局,我的应用程序中没有任何纵向布局,也没有任何布局文件夹好吧,我还在清单 activity 中添加了 screenOrientation="landscape"。

Activity 代码如下:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

Manifest.xml 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yasir.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

项目结构

FATAL EXCEPTION: main  
Process: com.example.yasir.sample, PID: 12611

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.yasir.sample/com.example.yasir.sample.MainActivity}: 
android.content.res.Resources$NotFoundException: Resource ID #0x7f04001b
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3151)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5205)
at android.app.ActivityThread.access00(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1741)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f04001b
at android.content.res.Resources.getValue(Resources.java:2495)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:4236)
at android.content.res.Resources.getLayout(Resources.java:2311)
at android.view.LayoutInflater.inflate(LayoutInflater.java:413)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.yasir.sample.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3104)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5205) 
at android.app.ActivityThread.access00(ActivityThread.java:219) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1741) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6939) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

Resources$NotFoundException

This exception is thrown by the resource APIs when a requested resource can not be found

仅供参考

  • 移除 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

  • 在清单部分添加android:configChanges="orientation|screenLayout"

Finally

<activity android:name=".MainActivity"
                android:screenOrientation="landscape"
                 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout">

然后Clean and Rebuild你的项目。

https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/

这篇 link 将解释如何防止 AsyncTaskScreen Orentation

导致的崩溃