简体   繁体   English

RuntimeException 找不到布局 XML 资源

[英]RuntimeException can't find layout XML resource

I've tried to implement a loading screen for my android app.我尝试为我的 android 应用程序实现加载屏幕。 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.我是通过 APK 安装应用程序还是直接使用 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崩溃的 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

<?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 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.我正在尝试安装该应用程序的手机正在运行 Android 6.0.1。

I've also noticed that I have two res folders in my workspace (One marked as generated).我还注意到我的工作区中有两个 res 文件夹(一个标记为已生成)。 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:更改您的 Splash 背景 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
         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.问题是我只在 v24 文件夹中创建了我的可绘制资源。 This caused the "Resources not found" Error on older api versions which only used the resources from the not v24 folder.这导致旧 api 版本出现“找不到资源”错误,该版本仅使用非 v24 文件夹中的资源。

Hopefully this helps, other people who made the same mistake.希望这对犯同样错误的其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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