ResourceNotFoundException 仅在 Android 模拟器中的某些屏幕分辨率下

ResourcesNotFoundException only on some screen resolutions in the Android Emulator

我使用 AVD 创建了一个 7 英寸屏幕分辨率为 2880x1800 的虚拟设备。我的目标是获得高分辨率的 7 英寸屏幕截图。但是我的应用无法在此设备上加载。日志显示 setContentView(R.layout.my_activity_layout) 处的资源未找到。这是怎么回事?它适用于从普通到超大型的所有其他设备。有任何想法吗?

这是我的堆栈跟踪。

04-21 21:31:52.513    2380-2380/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.nsouthproductions.mathanimalsaddition, PID: 2380
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nsouthproductions.mathanimalsaddition/com.nsouthproductions.mathanimalsaddition.Main}: android.content.res.Resources$NotFoundException: Resource ID #0x7f040008
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f040008
            at android.content.res.Resources.getValue(Resources.java:1233)
            at android.content.res.Resources.loadXmlResourceParser(Resources.java:2598)
            at android.content.res.Resources.getLayout(Resources.java:1049)
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:870)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
            at android.app.Activity.setContentView(Activity.java:2144)
            at com.nsouthproductions.mathanimalsaddition.Main.onCreate(Main.java:333)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-21 21:31:52.740      933-933/? E/EGL_emulation﹕ tid 933: eglCreateSyncKHR(1237): error 0x3004 (EGL_BAD_ATTRIBUTE)

编辑: 添加 Java 和 XML,尽管它们非常标准并且被证明可以在其他设备上工作。

Java:(错误发生在setContentView()

@SuppressLint("NewApi")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Ensures they resume where left off if hit launcher and game already playing
        // If the launcher is trying to create a new instance of Main, this checks to see
        //   if this is a new instance of the app. If not, close this activity so that the
        //   one below it (where they left off) is at the top.
        if (!isTaskRoot()
                && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
                && getIntent().getAction() != null
                && getIntent().getAction().equals(Intent.ACTION_MAIN)) {
            finish();
            return;
        }

        Intent intent = getIntent();

        if(savedInstanceState != null){
            START_IN_PLAY = savedInstanceState.getBoolean(KEY_SHOWING_PLAY, false);
            START_IN_PRACTICE = savedInstanceState.getBoolean(KEY_SHOWING_PRACTICE, false);
            START_IN_QUIZ = savedInstanceState.getBoolean(KEY_SHOWING_QUIZ, false);
        }

        context = this;

        mDecorView = getWindow().getDecorView();
        // call before setContentView to avoid system bars briefly showing
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        System.out.println("checking os version");
        if (currentapiVersion >= Build.VERSION_CODES.KITKAT) {
            System.out.println("KitKat or newer");
            mDecorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            );
        } else{
            System.out.println("Older than KitKat");
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        }


        setContentView(R.layout.activity_main);

main_layout land 资源

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" tools:context=".Main"
    android:id="@+id/area_main_layout">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="2">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:id="@+id/imageView6"
            android:src="@drawable/title_math_animals"
            android:cropToPadding="false"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/imageView7"
            android:src="@drawable/title_addition"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_marginBottom="15dp"
        android:layout_weight="3"
        android:id="@+id/linlay_main_2">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_play"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_play"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img_play_text"
                android:src="@drawable/text_play"
                android:layout_centerInParent="true" />
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_practice"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_practice_text"
                android:src="@drawable/text_practice"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_quiz"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_quiz_text"
                android:src="@drawable/text_quiz"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_animals"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_animals_text"
                android:src="@drawable/text_animals"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>
    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->

</LinearLayout>

main_layout land 资源

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" tools:context=".Main"
    android:id="@+id/area_main_layout">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="2">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView6"
            android:src="@drawable/title_math_animals"
            android:cropToPadding="false"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="7dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:id="@+id/imageView7"
            android:src="@drawable/title_addition"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_marginBottom="10dp"
        android:layout_weight="3"
        android:id="@+id/linlay_main_2">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_play"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_play"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img_play_text"
                android:src="@drawable/text_play"
                android:layout_centerInParent="true" />
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_practice"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_practice_text"
                android:src="@drawable/text_practice"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_quiz"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_quiz_text"
                android:src="@drawable/text_quiz"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_animals"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_animals_text"
                android:src="@drawable/text_animals"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>
    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->

</LinearLayout>

main_layout large_land 资源

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" tools:context=".Main"
    android:id="@+id/area_main_layout">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="3"
        android:layout_marginTop="15dp">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:id="@+id/imageView6"
            android:src="@drawable/title_math_animals"
            android:cropToPadding="false"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            android:id="@+id/imageView7"
            android:src="@drawable/title_addition"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_marginBottom="15dp"
        android:layout_weight="6"
        android:id="@+id/linlay_main_2"
        android:layout_marginTop="10dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_play"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_play"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img_play_text"
                android:src="@drawable/text_play"
                android:layout_centerInParent="true" />
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_practice"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_practice_text"
                android:src="@drawable/text_practice"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_quiz"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_quiz_text"
                android:src="@drawable/text_quiz"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:id="@+id/btn_animals"
                android:layout_centerInParent="true"
                android:soundEffectsEnabled="false"
                android:background="@drawable/selector_btn_menu_other"  />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="28dp"
                android:id="@+id/img_animals_text"
                android:src="@drawable/text_animals"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>
    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->

</LinearLayout>

我不能 post xlarge-land 不超过字符限制。我已经看到所有这些资源都在其他设备上成功加载。

对于任何资源,您始终必须提供默认版本(在不合格的资源目录中)作为系统无法确定最合适的资源(或没有与当前配置匹配的资源)时的后备.

有关详细信息,请参阅 Providing Resources - Compatibility

Likewise, if you provide different layout resources based on the screen orientation, you should pick one orientation as your default. For example, instead of providing layout resources in layout-land/ for landscape and layout-port/ for portrait, leave one as the default, such as layout/ for landscape and layout-port/ for portrait.