android - 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);
}
}
解决方案
推荐阅读
- python - 对所有 GUI 小部件应用一致的填充
- javascript - HighCharts/Javascript:将月度数据系列重新采样为季度/年度?
- javascript - JS - 如何将 cookie 更新到同一个站点 无
- scala - 如何使用 scala 的 List::groupBy 的结果?
- javascript - 如何模拟文件,用茉莉花隐藏?
- python-3.x - 从 pyspark 数据框中的列返回字母数字值
- linux - 使用来自主机的 CA Trust Bundle 构建 Docker
- loops - 原生 Google 表格中的嵌套“for”循环(无脚本)
- java - Java 8 Lambda 映射
到地图 > - angular - 在角度上动态更改 LOCALE_ID