错误膨胀 class ImageButton 停止应用程序启动

Error inflating class ImageButton Stops app from starting

我的应用程序因膨胀 class ImageButton 错误而无法启动,因此 phone 显示 "app has stopped" 等等。 我正在使用 firebase 测试实验室来调试问题

错误发生在三星设备上,特别是 A20、s7 Edge、s7(使用 firebase 测试同样的问题)

我是 android 的新手,已经尝试了 google 的许多解决方案,因此 none 目前有效

这是关于 firebase 的错误报告

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartpractice.smartpracticesmartapp/com.smartpractice.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #117: Binary XML file line #117: Error inflating class ImageButton
     FATAL EXCEPTION: main
Process: com.smartpractice.smartpracticesmartapp, PID: 26921
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartpractice.smartpracticesmartapp/com.smartpractice.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #117: Binary XML file line #117: Error inflating class ImageButton
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
    at android.app.ActivityThread.access00(ActivityThread.java:221)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #117: Binary XML file line #117: Error inflating class ImageButton
    at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.smartpractice.myapplication.MainActivity.onCreate(MainActivity.java:14)
    at android.app.Activity.performCreate(Activity.java:6876)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
    ... 9 more
Caused by: android.view.InflateException: Binary XML file line #117: Error inflating class ImageButton
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
    ... 17 more
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070089
    at android.content.res.Resources.getValue(Resources.java:2558)
    at androidx.appcompat.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
    at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
    at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
    at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
    at androidx.appcompat.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
    at androidx.appcompat.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:79)
    at androidx.appcompat.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:69)
    at androidx.appcompat.app.AppCompatViewInflater.createImageButton(AppCompatViewInflater.java:202)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:122)
    at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
    at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758)

这是XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/ic_launcher_background"
    tools:context=".MainActivity">

    <com.google.android.flexbox.FlexboxLayout
        android:id="@+id/flexboxLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:alignContent="stretch"
        app:alignItems="stretch"
        app:flexWrap="wrap"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.863"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline7">


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:fontFamily="@font/nunito"
            android:gravity="center_horizontal"
            android:text="@string/welcome_to_smartpractice"
            android:textSize="24sp"
            app:fontFamily="@font/nunito"
            app:layout_alignSelf="baseline"
            tools:layout_editor_absoluteY="100dp"
            tools:targetApi="jelly_bean" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark"
            android:gravity="center_horizontal|fill_vertical"
            android:text="@string/docsharesmart"
            android:textColor="@color/ic_launcher_background"
            app:layout_alignSelf="flex_end"
            tools:layout_editor_absoluteY="16dp" />

    </com.google.android.flexbox.FlexboxLayout>

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:alignContent="center"
        app:alignItems="stretch"
        app:flexDirection="row"
        app:flexWrap="nowrap"
        app:justifyContent="space_between"
        app:layout_constraintBottom_toTopOf="@+id/guideline7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/logo1"
            app:layout_alignSelf="center"
            app:srcCompat="@drawable/smartpractice_logo_02"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="-46dp" />

        <ImageButton
            android:id="@+id/LoginScreen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@null"
            android:contentDescription="@string/loginscreen"
            app:layout_alignSelf="center"
            app:layout_wrapBefore="false"
            app:srcCompat="@drawable/ic_more_vert_black_24dp"
            tools:layout_editor_absoluteX="248dp"
            tools:layout_editor_absoluteY="134dp" />


    </com.google.android.flexbox.FlexboxLayout>

    <com.google.android.flexbox.FlexboxLayout
        android:id="@+id/flexboxLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:alignContent="stretch"
        app:alignItems="stretch"
        app:flexWrap="wrap"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/flexboxLayout"
        app:layout_constraintVertical_bias="0.146">

这是错误的第 117 行

        <ImageButton
            android:id="@+id/UploadButton"
            android:layout_width="300dp"
            android:layout_height="209dp"
            android:background="@android:color/transparent"
            app:layout_constraintTop_toBottomOf="@+id/flexboxLayout"
            app:srcCompat="@drawable/upload_files_upload"
            android:contentDescription="@string/todo" />


    </com.google.android.flexbox.FlexboxLayout>


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="185dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

您出错的原因是 upload_files_upload 不在您的 drawable 文件夹中:

<ImageButton
            android:id="@+id/UploadButton"
            android:layout_width="300dp"
            android:layout_height="209dp"
            android:background="@android:color/transparent"
            app:layout_constraintTop_toBottomOf="@+id/flexboxLayout" 
            app:srcCompat="@drawable/upload_files_upload"// this line
            android:contentDescription="@string/todo" />

这意味着 upload_files_upload 在 drawble 文件夹中找不到,这可能是一种情况,您的文件可能存在于 drawable-24 但不在 drawble 文件夹中,那样的话还是会出现这个问题。重点是 您的文件必须在所有 drawble 文件夹中。

我已经解决了我将 app:srcCompat="@drawable/upload_files_upload"// 更改为 android:src="@drawable/upload_files_upload" 的问题 所以问题出在 app:srcCompat 我将其更改为 Android:src 现在可以正常工作了