首页 > 解决方案 > RuntimeException:无法启动活动错误膨胀类 ImageView 和 ImageButton

问题描述

我不知道,我已经看过其中的大多数,但仍然无法正常工作。这个问题只出现在 Vivo、OPPO、华为、LGE、Trend、摩托罗拉等少数设备上......

我把png放在hdpi、mdpi、xhdpi、xxhdpi、xxxhdpi文件夹中,把SVG放在drawable文件夹中,没有v24文件...

不仅 png 会发生,SVG 也会发生

堆栈跟踪

Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{com.newec.mysport/com.newec.mysport.view.main.HomeActivity}: android.view.InflateException: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Error inflating class ImageView
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.newec.mysport/com.newec.mysport.view.main.HomeActivity}: android.view.InflateException: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Error inflating class ImageView
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3520)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3659)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2245)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7840)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:985)
Caused by android.view.InflateException
Binary XML file line #76 in com.newec.mysport:layout/activity_home: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Error inflating class ImageView
Caused by android.view.InflateException: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Error inflating class ImageView

Caused by android.view.InflateException
Binary XML file line #76 in com.newec.mysport:layout/activity_home: Error inflating class ImageView
Caused by android.view.InflateException: Binary XML file line #76 in com.newec.mysport:layout/activity_home: Error inflating class ImageView

Caused by android.content.res.Resources$NotFoundException
Drawable (missing name) with resource ID #0x7f080145
Caused by android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID #0x7f080145

Caused by android.content.res.Resources$NotFoundException
Unable to find resource ID #0x7f080145
Caused by android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f080145
       at android.content.res.ResourcesImpl.getResourceName(ResourcesImpl.java:298)
       at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:856)
       at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:702)
       at android.content.res.Resources.loadDrawable(Resources.java:924)
       at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:1005)
       at android.content.res.TypedArray.getDrawable(TypedArray.java:980)
       at android.widget.ImageView.<init>(ImageView.java:212)
       at android.widget.ImageView.<init>(ImageView.java:193)
       at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
       at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
       at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:187)
       at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107)
       at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
       at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
       at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1063)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:999)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1086)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:1128)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1086)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:1128)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1086)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:1128)
       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1086)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
       at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
       at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
       at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
       at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
       at com.newec.mysport.view.main.HomeActivity$binding$2.invoke(HomeActivity.kt:36)
       at com.newec.mysport.view.main.HomeActivity$binding$2.invoke(HomeActivity.kt:35)
       at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
       at com.newec.mysport.view.main.HomeActivity.getBinding(:2)
       at com.newec.mysport.view.main.HomeActivity.onCreate(HomeActivity.kt:40)
       at android.app.Activity.performCreate(Activity.java:7953)
       at android.app.Activity.performCreate(Activity.java:7942)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3495)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3659)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2245)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7840)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:985)

布局代码

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
    </data>

    <androidx.drawerlayout.widget.DrawerLayout>

        <androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:contentInsetEnd="0dp"
                app:contentInsetStart="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageButton
                        android:id="@+id/img_navigation"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:background="@null"
                        android:padding="8dp"
                        android:scaleType="fitCenter"
                        android:src="@drawable/ic_navigation"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <ImageButton
                        android:id="@+id/img_back"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:background="@null"
                        android:src="@drawable/selec_back"
                        android:visibility="gone"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <ImageButton
                        android:id="@+id/btn_search"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_marginEnd="8dp"
                        android:background="@null"
                        android:onClick='@{()->viewmodel.goSearch("","")}'
                        android:src="@drawable/selec_search"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintDimensionRatio="H,5:4"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <ImageView
                        android:id="@+id/img_logo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_logo"
                        android:visibility="@{viewmodel.title.isEmpty()?View.VISIBLE:View.GONE}"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="@id/guideline_mid"
                        app:layout_constraintStart_toStartOf="@id/guideline_mid"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView/>
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.appcompat.widget.Toolbar>

            <fragment/>

            <com.google.android.material.bottomnavigation.BottomNavigationView/>

            <androidx.viewpager.widget.ViewPager/>

            <com.google.android.material.tabs.TabLayout/>

            <TextView/>

            <androidx.constraintlayout.widget.Group/>
        </androidx.constraintlayout.widget.ConstraintLayout>

        <include/>
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

ImageButton img_navigation

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
  <path
      android:pathData="M21,6c0,0.552 -0.448,1 -1,1H4c-0.552,0 -1,-0.448 -1,-1s0.448,-1 1,-1h16c0.552,0 1,0.448 1,1zM12,18c0,0.552 -0.448,1 -1,1H4c-0.552,0 -1,-0.448 -1,-1s0.448,-1 1,-1h7c0.552,0 1,0.448 1,1zM21,12c0,0.552 -0.448,1 -1,1H4c-0.552,0 -1,-0.448 -1,-1s0.448,-1 1,-1h16c0.552,0 1,0.448 1,1z"
      android:fillColor="#2563AE"
      android:fillType="evenOdd"/>
</vector>

图像按钮。btn_search

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_search_gray" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_search_gray" android:state_focused="true" />
    <item android:drawable="@drawable/ic_search_gray" android:state_checked="true" />
    <item android:drawable="@drawable/ic_search_primary" />
</selector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="#888"
        android:fillType="evenOdd"
        android:pathData="M10,16c3.314,0 6,-2.686 6,-6s-2.686,-6 -6,-6 -6,2.686 -6,6 2.686,6 6,6zM10,18c4.418,0 8,-3.582 8,-8s-3.582,-8 -8,-8 -8,3.582 -8,8 3.582,8 8,8zM20.707,21.293c-0.39,0.39 -1.024,0.39 -1.414,0l-2.586,-2.586c-0.39,-0.39 -0.39,-1.024 0,-1.414 0.39,-0.39 1.024,-0.39 1.414,0l2.586,2.586c0.39,0.39 0.39,1.023 0,1.414z" />
</vector>

我的可绘制导演

https://i.stack.imgur.com/HuC4a.png

标签: androidandroid-layoutkotlin

解决方案


您的问题是,设备无法找到可绘制的ic_logo,很可能您没有根据设备的屏幕密度创建可绘制目录,您正在尝试启动您的应用程序。您应该将每个可绘制对象单独放在可绘制目录中,以便如果任何屏幕密度不匹配,它仍然可以加载该通用可绘制对象。

如果你的drawable ic_logo是SVG,那么你不能直接使用它,你必须在xml中导入它。在Project 窗口中,右键单击res 目录并选择New > Vector Asset


推荐阅读