android 4.2.2 中未找到资源异常

Resource not found exception in android 4.2.2

我已经在 style.xml 文件中为闪屏创建了一个主题。

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="android:windowBackground">@drawable/splash_background_96dp</item>
</style>

splash_background_96dp 是一个 xml 可绘制对象。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorWhite"/>

    <item android:gravity="center">
        <bitmap
            android:gravity="fill_horizontal|fill_vertical"
            android:src="@drawable/logo1"/>
    </item>
</layer-list>

logo1 是一个 png 文件。

我在我的应用程序中将此主题用作初始屏幕 activity。它适用于 android 7.1.0 但在 android 4.2.2 上会发生以下异常:

03-04 15:31:02.849 14225-14225/com.sarsabad.sarsabadmanager E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sarsabad.sarsabadmanager/com.sarsabad.sarsabadmanager.ui.SplashActivity}: android.content.res.Resources$NotFoundException: File res/drawable/splash_background_96dp.xml from drawable resource ID #0x7f0700e4
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
                                                                                  at android.app.ActivityThread.access0(ActivityThread.java:150)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                  at android.os.Looper.loop(Looper.java:137)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5283)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                                               Caused by: android.content.res.Resources$NotFoundException: File res/drawable/splash_background_96dp.xml from drawable resource ID #0x7f0700e4
                                                                                  at android.content.res.Resources.loadDrawable(Resources.java:2842)
                                                                                  at android.content.res.Resources.getDrawable(Resources.java:1521)
                                                                                  at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:360)
                                                                                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
                                                                                  at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:85)
                                                                                  at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:128)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
                                                                                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
                                                                                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
                                                                                  at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                                                  at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:70)
                                                                                  at com.sarsabad.sarsabadmanager.ui.SplashActivity.onCreate(SplashActivity.java:39)
                                                                                  at android.app.Activity.performCreate(Activity.java:5283)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299) 
                                                                                  at android.app.ActivityThread.access0(ActivityThread.java:150) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                  at android.os.Looper.loop(Looper.java:137) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5283) 
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 
                                                                               Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: <bitmap> requires a valid src attribute
                                                                                  at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:454)
                                                                                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:921)
                                                                                  at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:165)
                                                                                  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:921)
                                                                                  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:858)
                                                                                  at android.content.res.Resources.loadDrawable(Resources.java:2839)
                                                                                  at android.content.res.Resources.getDrawable(Resources.java:1521) 
                                                                                  at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:360) 
                                                                                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198) 
                                                                                  at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:85) 
                                                                                  at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:128) 
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149) 
                                                                                  at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29) 
                                                                                  at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54) 
                                                                                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202) 
                                                                                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183) 
                                                                                  at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) 
                                                                                  at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:70) 
                                                                                  at com.sarsabad.sarsabadmanager.ui.SplashActivity.onCreate(SplashActivity.java:39) 
                                                                                  at android.app.Activity.performCreate(Activity.java:5283) 
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299) 
                                                                                  at android.app.ActivityThread.access0(ActivityThread.java:150) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                  at android.os.Looper.loop(Looper.java:137) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5283) 
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 

为了使 logo1 文件可用,您需要将图像放入 drawable 文件夹中,以便所有 Android 版本都可以访问。正如我猜对的那样,该图像位于其他具有更高限定符的文件夹中(即在您的情况下为 drawable-v21 )。