Search code examples
javaandroidandroid-imageview

Error inflating class ImageView in android


whenever i hit the imageview i get this error, this happens to me when i switch to test my app on different screen sizes, here is my xml

    <ImageView
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"

        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="28dp"
        android:layout_marginRight="28dp"
        android:layout_marginTop="20dp"
        app:srcCompat="@drawable/search"/>

and here is the error

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.ahmed.electionadmin, PID: 30789
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.electionadmin/com.example.ahmed.electionadmin.Search}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)

I have tried so many solutions that asks me to replace src with srccompat and so on, but still is working, ant help would be appreciated


Solution

  • To use srcCompat you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file:

     android {  
       defaultConfig {  
         vectorDrawables.useSupportLibrary = true  
        }  
     } 
    

    Or you can use src instead of srcCompat to resolve the error.

    android:src="@drawable/search"