0

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

ahmed
  • 45
  • 1
  • 10

4 Answers4

2

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"
Sheikh Hasib
  • 7,423
  • 2
  • 25
  • 37
1

Replace this:

app:srcCompat="@drawable/search"

With:

android:src="@drawable/search"

Pay attention, not only src with srcCompat, also app with android.

Let me know if it works.

Explanation: You're not using a support library ImageView, but a regular one. app namespace is usually used for support library, same as srcCompat. So you needed to change those 2 things, not just one. That's why merely changing the srcCompat with src yielded no results.

Vucko
  • 7,371
  • 2
  • 27
  • 45
1

i fixed it by uploading my image inside draawable no 24

ahmed
  • 45
  • 1
  • 10
0

you are doing like fitting image to different resolutions , you should have image with different resolutions like hdpi xhdpi , you you can use vector drawable image