0

I know this is a common question here but none of the solutions have worked in my case. First of all, here's the error log:

11-14 14:47:15.145 2717-2717/com.example E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example, PID: 2717
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.AudioActivity_}: android.view.InflateException: Binary XML file line #45: Error inflating class androidx.appcompat.widget.AppCompatImageView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class androidx.appcompat.widget.AppCompatImageView
        at android.view.LayoutInflater.createView(LayoutInflater.java:621)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at com.example.finder.music.MusicFragment.onCreateView(MusicFragment.java:104)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at com.example.BaseActivity.onStart(BaseActivity.java:225)
        at com.example.AudioActivity.onStart(AudioActivity.java:86)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
        at android.app.Activity.performStart(Activity.java:5241)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5017) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at android.view.LayoutInflater.createView(LayoutInflater.java:595)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
        at com.example.finder.music.MusicFragment.onCreateView(MusicFragment.java:104) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460) 
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) 
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802) 
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) 
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) 
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) 
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) 
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273) 
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) 
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) 
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620) 
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
        at com.example.BaseActivity.onStart(BaseActivity.java:225) 
        at com.example.AudioActivity.onStart(AudioActivity.java:86) 
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171) 
        at android.app.Activity.performStart(Activity.java:5241) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5017) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/lib_file_music.xml from drawable resource ID #0x7f0800fb
        at android.content.res.Resources.loadDrawable(Resources.java:2101)
        at android.content.res.Resources.getDrawable(Resources.java:700)
        at androidx.appcompat.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:130)
        at androidx.appcompat.widget.Tint

Here's the drawable in question (lib_file_music.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item
        android:bottom="20dp"
        android:drawable="@drawable/lib_ic_volume_up_24dp"
        android:left="40dp"
        android:right="40dp"
        android:top="20dp">
    </item>
</layer-list>

and the subsequent lib_ic_volume_up_24dp:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="#FFFFFF"
        android:pathData="M3,9v6h4l5,5L12,4L7,9L3,9zM16.5,12c0,-1.77 -1.02,-3.29 -2.5,-4.03v8.05c1.48,-0.73 2.5,-2.25 2.5,-4.02zM14,3.23v2.06c2.89,0.86 5,3.54 5,6.71s-2.11,5.85 -5,6.71v2.06c4.01,-0.91 7,-4.49 7,-8.77s-2.99,-7.86 -7,-8.77z" />
</vector>

It is included in an ImageView as:

<androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/music_cover"
                android:layout_width="180dp"
                android:layout_height="180dp"
                android:layout_marginBottom="25dp"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/lib_file_music" />

Compile and Target SDK is 28, min SDK is 18. The error occurs in APIs <21.

Here's a list of things I've tried so far based on the other similar threads on SO:

  • Making sure dependencies are up-to-date
  • Making sure drawable is in the right folder
  • Replacing ImageViews with AppCompatImageViews
  • Removing size attribute from drawable’s xml
  • Adding bitmap attribute to drawable’s xml
  • Adding AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) to activity
  • Adding android.defaultConfig.vectorDrawables.useSupportLibrary = true to gradle
  • Replacing android:src with app:srcCompat or android:background
  • Overriding resources in other configurations (v18, v19, v20)

  • And of course the standard clean/rebuild, clear caches/restart.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • What happen if you replace other image instead of `lib_file_music`? Will it crashed? – John Joe Nov 14 '19 at 15:33
  • @JohnJoe I tested it with a few other drawables and I was getting the same error messages. The drawables mentioned though have very similar code, they just reference a different icon. – Nikos Hidalgo Nov 14 '19 at 15:36
  • @NikosHidalgo, I have tried your code but unfortunately it didn't gave me any error. – Suraj Vaishnav Nov 14 '19 at 15:56
  • @SurajVaishnav Thank you for trying. I tried creating a new empty activity containing just that imageview but unfortunately, I'm still getting the same error. Did you use androidX and latest dependencies in your project? Also, what was the API version on the device/emulator where you tested it? – Nikos Hidalgo Nov 14 '19 at 16:08
  • I have tried in your code in another project but still doesn't gave any error, So my suggestions are: 1. my target sdk version is 29 (I think you should update it too.) 2. Here are the androidx related dependency in my build.gradle (app Level) `implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.core:core-ktx:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0'` So try to change as it was in my project, may resolve this. – Suraj Vaishnav Nov 14 '19 at 16:12
  • @NikosHidalgo Are you sure, your lib_file_music file present in **res->drawable** folder? – Suraj Vaishnav Nov 14 '19 at 16:20
  • @SurajVaishnav except for the SDK and the kotlin dependency which I'm not using, the other dependencies are the same. Also, I have checked and I only have one folder for drawable (the default one) and the file is in there. I even tried creating the drawable folders for older versions (eg drawable-v18) and include the file in them as well, but it still didn't solve my problem – Nikos Hidalgo Nov 14 '19 at 16:23
  • Then we have only one option, create a new project, then in activity_main.xml put your AppCompatImageView code there also drawable in drawble folder, run it if it runs successfully compare your current project to the old one. – Suraj Vaishnav Nov 14 '19 at 16:29

1 Answers1

4

First of all you should not use the AppCompatImageView directly. Just use ImageView. It will be replaced automatically when needed.

Did you setup vectorDrawables.useSupportLibrary = true in your build.gradle ?

Edit:
The layer list might be the problem on lower API level. Check this out Vector Drawable in Layer List on Older Android Versions

Alex
  • 3,382
  • 2
  • 32
  • 41
  • I had ImageView originally. I only replaced it after seeing a suggestion in a similar thread. I also have vectorDrawables.useSupportLibrary = true . I mention that alongside some other things I've tried towards the end of my question – Nikos Hidalgo Nov 14 '19 at 15:40
  • Would try then just using the vector drawable directly without the layer list first, see it that works or not... Vector drawables on lower API levels are always problematic. – Alex Nov 14 '19 at 15:42
  • You might be on to something. I pointed the imageview to the embedded drawable directly and it did display that just fine. The layer list might be the problem indeed. Please include that suggestion in your answer so I can upvote it. Although I would still hope that somebody could shed some light as to why the layer list is causing that problem. – Nikos Hidalgo Nov 14 '19 at 16:28
  • Also check this out https://stackoverflow.com/questions/42885410/vector-drawable-in-layer-list-on-older-android-versions – Alex Nov 14 '19 at 17:45