关于作者:CSDN内容合伙人、技术专家, 从零开始做日活千万级APP。
专注于分享各领域原创系列文章 ,擅长java后端、移动开发、商业变现、人工智能等,希望大家多多支持。
未经允许不得转载
目录
一、导读
我们继续总结学习遇到的问题,温故知新。
今天遇到一个线上问题,启动就闪退,比较坑,在此做一个记录,防止掉坑。
二、概览
我们在启动时做了一个启动图的优化,启动时让用户看到公司的logo,同时解决黑屏、白屏问题,这时也给用户一个假象,感觉启动起来很快一样。
但是上线后包了一些错误, 如下:
java
java.lang.RuntimeException:Unable to start activity ComponentInfo{com.qxx/com.qxx.Activity}: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
2
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:26913)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #51: <bitmap> requires a valid src attribute
java
java.lang.RuntimeException:Unable to start activity ComponentInfo{com.xx/com.xx.Activity}: android.content.res.Resources$NotFoundException: File res/xyadx.xml from drawable resource ID #xxxx
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:25211)
......
Caused by:
org.xmlpull.v1.XmlPullParserException:Binary XML file line #91: <bitmap> requires a valid src attribute
java
# main(1)
org.xmlpull.v1.XmlPullParserException
Binary XML file line #9: <bitmap> requires a valid 'src' attribute
java.lang.RuntimeException:Unable to start activity ComponentInfo{com.xxx/com.xxxActivity}: android.content.res.Resources$NotFoundException: Drawable com.xxx:drawable/loading_bg with resource ID #0x7xxf2
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:27681)
......
Caused by:
org.xmlpull.v1.XmlPullParserException:Binary XML file line #19: <bitmap> requires a valid 'src' attribute
三、问题记录
配置上中下三个image,再配个背景,更显高大上,
xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
全局背景
<item>
<color android:color="#FFFFFF" />
</item>
配置上中下三个image
<item>
<bitmap
android:gravity="top"
android:src="@drawable/loading_bg" />
</item>
<item
android:bottom="@dimen/dp_10">
<bitmap
android:gravity="center"
android:src="@drawable/loading_slogan" />
</item>
<item>
<bitmap
android:gravity="bottom"
android:src="@drawable/loading_buttom_logo" />
</item>
</layer-list>
我们一起看看源码报错的地方 BitmapDrawable.java
java
/**
* Updates the constant state from the values in the typed array.
*/
private void updateStateFromTypedArray(TypedArray a, int srcDensityOverride)
throws XmlPullParserException {
final Resources r = a.getResources();
final BitmapState state = mBitmapState;
final int srcResId = a.getResourceId(R.styleable.BitmapDrawable_src, 0);
if (srcResId != 0) {
...
一系列的操作后,根据resid去获取bitmap,如果bitmap为空,则会抛出异常
Bitmap bitmap = null;
try (InputStream is = r.openRawResource(srcResId, value)) {
ImageDecoder.Source source = ImageDecoder.createSource(r, is, density);
bitmap = ImageDecoder.decodeBitmap(source, (decoder, info, src) -> {
decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE);
});
} catch (Exception e) {
}
if (bitmap == null) {
throw new XmlPullParserException(a.getPositionDescription() +
": <bitmap> requires a valid 'src' attribute");
}
}
...
}
一系列的操作后,根据resid去获取bitmap,如果bitmap为空,则会抛出异常,
分析原因后,我们得出结论
- bitmap 使用 src来引用, android:src
- 的src不允许为xml drawables
- src引用的图片资源在多分辨率下都要有,不让容易出问题
四、 推荐阅读
未经允许不得转载