简体   繁体   中英

RuntimeException can't find layout XML resource

I've tried to implement a loading screen for my android app. My application works perfectly fine on many devices. I managed to install it on several devices, but one. On one phone the app crashes instantly, when I try to start it. It doesn't matter if I install the app via APK or directly with android studio.

Here is the error message :

10-19 16:18:35.088 19315-19315/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.jp.einkaufszettellisten, PID: 19315
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jp.einkaufszettellisten/com.jp.einkaufszettellisten.Activities.Ladebildschirm}: android.content.res.Resources$NotFoundException: File res/drawable/splash_background.xml from drawable resource ID #0x7f070095
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2518)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2580)
        at android.app.ActivityThread.access$1200(ActivityThread.java:162)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5678)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/splash_background.xml from drawable resource ID #0x7f070095
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2841)
        at android.content.res.Resources.loadDrawable(Resources.java:2710)
        at android.content.res.Resources.getDrawable(Resources.java:862)
        at android.content.Context.getDrawable(Context.java:458)
        at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:454)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:144)
        at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:411)
        at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86)
        at androidx.appcompat.app.AppCompatDelegateImpl.attachToWindow(AppCompatDelegateImpl.java:647)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureWindow(AppCompatDelegateImpl.java:623)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreate(AppCompatDelegateImpl.java:350)
        at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:105)
        at com.jp.einkaufszettellisten.Activities.Ladebildschirm.onCreate(Ladebildschirm.java:20)
        at android.app.Activity.performCreate(Activity.java:6324)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2471)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2580) 
        at android.app.ActivityThread.access$1200(ActivityThread.java:162) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5678) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #9: <bitmap> requires a valid 'src' attribute
        at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:764)
        at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:724)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
        at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:254)
        at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:164)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2818)
        at android.content.res.Resources.loadDrawable(Resources.java:2710) 
        at android.content.res.Resources.getDrawable(Resources.java:862) 
        at android.content.Context.getDrawable(Context.java:458) 
        at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:454) 
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:144) 
        at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:411) 
        at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86) 
        at androidx.appcompat.app.AppCompatDelegateImpl.attachToWindow(AppCompatDelegateImpl.java:647) 
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureWindow(AppCompatDelegateImpl.java:623) 
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreate(AppCompatDelegateImpl.java:350) 
        at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:105) 
        at com.jp.einkaufszettellisten.Activities.Ladebildschirm.onCreate(Ladebildschirm.java:20) 
        at android.app.Activity.performCreate(Activity.java:6324) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2471) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2580) 
        at android.app.ActivityThread.access$1200(ActivityThread.java:162) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5678) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

The activity in Manifest

<activity
            android:name=".Activities.Ladebildschirm"
            android:label="@string/app_name"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

The crashing class

package com.jp.einkaufszettellisten.Activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class Ladebildschirm extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        startActivity(new Intent(Ladebildschirm.this, MainActivity.class));
        finish();
    }
}

Splash background xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:opacity="opaque">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@color/backgroundLight"/>
    <!-- Your product logo - 144dp color version of your app icon -->
    <item>
        <bitmap
            android:gravity="center"
            app:srcCompat="@drawable/ic_launcher"
            android:src="@drawable/ic_launcher"/>
    </item>
</layer-list>

Style resource file

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="SplashTheme">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.jp.einkaufszettellisten"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 2
        versionName '2.0.0'
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.github.yukuku:ambilwarna:2.0.1'
}

The phone I'm trying to install the app has Android 6.0.1 running.

I've also noticed that I have two res folders in my workspace (One marked as generated). The generated folder is empty. I don't know if it has something to do with my problem.

Make changes to your Splash background XML file as below:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:opacity="opaque">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@color/backgroundLight"/>
    <!-- Your product logo - 144dp color version of your app icon -->
    <item
         android:gravity="center"
         android:drawable="@drawable/ic_launcher" />
</layer-list>

I found the answer to the problem.

The problem was that I've created my drawable resources only in the v24 folder. This caused the "Resources not found" Error on older api versions which only used the resources from the not v24 folder.

Hopefully this helps, other people who made the same mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM