【问题标题】:ResourcesNotFoundException only on some screen resolutions in the Android EmulatorResourceNotFoundException 仅适用于 Android 模拟器中的某些屏幕分辨率
【发布时间】:2015-04-22 10:11:42
【问题描述】:

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

这是我的堆栈跟踪。

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.access$800(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.access$800(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_layoutland 资源

<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_layoutland 资源

<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>

如果不超过字符数限制,我无法为xlarge-land 发帖。我已经看到所有这些资源都在其他设备上成功加载。

【问题讨论】:

  • 发布您的 xml 和 java 代码。
  • res/values目录下有my_activity_layout.xml
  • @kcoppock,不,我没有。我在res/layout(及其变体)中有它。
  • @dora,我已经更新了我的 xml 和 java 代码,但我知道我的资源可以在其他设备上运行,即使找不到所需的资源,我也希望 Android 选择下一个可用的最佳资源。
  • 对不起,我的意思是res/layout :) 所以你有默认的吗?不是其中一种变体——只是res/layout/my_activity_layout.xmlmy_activity_layout 是否包含另一个在res/layout 中不存在的布局?

标签: android android-resources android-screen-support avd


【解决方案1】:

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

更多信息请见Providing Resources - Compatibility

同样,如果您根据 屏幕方向,您应该选择一个方向作为默认方向。 例如,而不是在 layout-land/ 中提供布局资源 for 横向和 layout-port/ 用于纵向,保留一个作为默认值, 例如 layout/ 用于横向和 layout-port/ 用于纵向。

【讨论】:

  • 谢谢!我确保每个布局资源都有一个默认值,现在应用程序不会崩溃。不幸的是,我的科学怪人设备呈现非常奇怪 - 一切都被放大和低分辨率,甚至是股票主屏幕。但这可能是设置设备时的错误或错误。谢谢你教我一些有价值的东西:)
猜你喜欢
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
  • 2015-12-03
相关资源
最近更新 更多