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

时间:2017-10-10 06:22:05

标签: android layout crash landscape

平板电脑/移动设备更改后,应用程序在重新启动时崩溃,

我将仅以横向模式开发应用程序,并且我在layout-land中创建了一个名为acitivty_main的布局,并且我的应用程序中没有任何纵向布局,也没有任何布局文件夹,我还添加了screenOrientation =清单活动中的“横向”。

活动代码关注:

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>

项目结构

Project Structure

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.access$1100(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.access$1100(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) 

2 个答案:

答案 0 :(得分:3)

Resources$NotFoundException

  

请求时,资源API会抛出此异常   资源无法找到

<强> FYI

  • 移除 的 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 您的项目。

答案 1 :(得分:0)

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

此链接将说明如何防止来自AsyncTaskScreen Orentation

的崩溃