首页 > 解决方案 > Android ResourcesNotFoundException 但是图像存在于所有可绘制文件夹中

问题描述

一位 android 用户在 Nexus 5X 设备上出现了崩溃报告:

Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{package.MainActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
android.app.ActivityThread.performLaunchActivity
Caused by android.view.InflateException
Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
Caused by android.view.InflateException
Binary XML file line #8: Error inflating class ImageView

Caused by android.content.res.Resources$NotFoundException
Drawable (missing name) with resource ID #0x7f0700b8

Caused by android.content.res.Resources$NotFoundException
Unable to find resource ID #0x7f0700b8

指向 Mainactivity 中的第 61 行:MainActivity.onCreate (MainActivity.java:61),也就是这一行:

 this.setContentView(R.layout.activity_main);

我发现资源 ID #0x7f0700b8 指向我的图像 logo.png,它存在于所有可绘制文件夹(drawable、hdpi、ldpi、mdpi、xhdpi、xxhdpi、xxxhdpi)中,所以我不明白为什么 drawable 是没找到或者是什么问题。

XML 中的 imageview 如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bgmain">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:contentDescription="@string/hrady_a_zamky"
        android:paddingLeft="32dp"
        android:paddingRight="32dp"
        android:paddingTop="0dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/logo" />

和主要活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Locale locale = new Locale(lng);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
  this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());

    this.setContentView(R.layout.activity_main);

    ImageView language = findViewById(R.id.lang);
    ImageView logo = findViewById(R.id.logo);
    ImageView award = findViewById(R.id.award);

    if (lng.equals("sk")) {
        language.setImageResource(R.drawable.en);
        logo.setImageResource(R.drawable.logo);
    }

    else { language.setImageResource(R.drawable.sk);
        logo.setImageResource(R.drawable.logoen);
    }

}

标签: androidandroid-resources

解决方案


推荐阅读