每当我点击imageview时,我都会收到此错误,当我切换到在不同屏幕尺寸上测试我的应用时,这会发生在我身上,这是我的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"/>
这是错误
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)
我已经尝试了很多解决方案,要求我用srccompat替换src等,但是仍然可以正常工作,请提供蚂蚁帮助
最佳答案
要使用srcCompat
,您需要将vectorDrawables.useSupportLibrary = true
添加到build.gradle文件中:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
或者,您可以使用
src
代替srcCompat
来解决该错误。android:src="@drawable/search"