启动画面错误:android.content.res.Resources $ NotFoundException:可绘制

时间:2019-01-05 15:53:49

标签: android kotlin drawable splash-screen

我正在尝试通过以下方式为我的android应用实现启动屏幕 this article。我已逐步遵循它,但是每当我尝试运行该应用程序时,它都会立即崩溃,并且在我的logcat中出现以下错误:

2019-01-05 10:46:33.682 2265-2265/com.example.khoi.parkingapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.khoi.parkingapp, PID: 2265
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.khoi.parkingapp/com.example.khoi.parkingapp.SplashActivity}: android.content.res.Resources$NotFoundException: Drawable com.example.khoi.parkingapp:drawable/splash_background with resource ID #0x7f07008d
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.khoi.parkingapp:drawable/splash_background with resource ID #0x7f07008d
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/splash_background.xml from drawable resource ID #0x7f07008d
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:837)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659)
        at android.content.res.Resources.getDrawableForDensity(Resources.java:891)
        at android.content.res.Resources.getDrawable(Resources.java:833)
        at android.content.Context.getDrawable(Context.java:605)
        at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
        at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86)
        at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:260)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
        at com.example.khoi.parkingapp.SplashActivity.onCreate(SplashActivity.kt:10)
        at android.app.Activity.performCreate(Activity.java:7183)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #8: <bitmap> requires a valid 'src' attribute
        at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:823)
        at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:754)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:146)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1322)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1311)
        at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:284)
        at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:194)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:146)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1322)
2019-01-05 10:46:33.682 2265-2265/com.example.khoi.parkingapp E/AndroidRuntime:     at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1281)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:824)
            ... 24 more

这是我的 splash_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:drawable="@color/colorPrimary"/>

    <item>
        <bitmap
                android:gravity="center"
                android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:7)

就我而言,此解决方案无法解决问题。为了解决这个问题,我声明了初始屏幕的背景图像,如下所示:

<item android:drawable="@color/colorPrimary" />

<item
    android:drawable="@drawable/ic_launcher"
    android:gravity="center" />

答案 1 :(得分:5)

感谢Alexander缩小了错误范围,我发现了这个post可以解决我的问题:

我已将 splash_background.xml 修复为此:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:drawable="@color/colorPrimary"/>

    <item>
        <bitmap
                android:gravity="center"
                android:src="@drawable/ic_launcher"/>
    </item>

</layer-list>

显然,您需要将src放在drawable文件夹中,并且它是.png而不是.xml

答案 2 :(得分:1)

如果您在此处查看此行:

  

原因:org.xmlpull.v1.XmlPullParserException:二进制XML文件第8行:需要有效的“ src”属性

问题似乎是布局中可绘制对象的无效来源。

答案 3 :(得分:0)

如今,许多人正在使用具有多个图像的mipmaps-如果要使用矢量资产,则用以下代码替换您的item标签(在“ background_splash.xml”中具有位图的标签或任何带有此名称的标签):

<item
    android:height="48dp"
    android:width="48dp"
    android:drawable="@mipmap/ic_launcher"
    android:gravity="center" />

请注意,高度/宽度是我自己的,可以替换。此外,这仅适用于API级别23及更高级别。如果使用更低版本,则可以执行以下操作:

<item
    android:top="48dp"
    android:bottom="48dp"
    android:left="48dp"
    android:right="48dp"
    android:drawable="@mipmap/ic_launcher"
    android:gravity="center" />