我试图使用ImageButton
android:background="?attr/selectableItemBackground"
,但我的应用程序崩溃了。我只关注此answer和此question。有什么想法吗?
感谢您的帮助。
activity.xml
<ImageButton
android:id="@+id/signUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/signIn"
android:layout_alignParentBottom="true"
android:layout_marginBottom="80dp"
android:background="?attr/selectableItemBackground"
android:src="@drawable/sign_up"
android:contentDescription="@string/button_sign_up"
/>
Log Cat
05-31 18:50:24.077: W/dalvikvm(28619): threadid=1: thread exiting with uncaught exception (group=0x41d0b2a0)
05-31 18:50:24.082: E/AndroidRuntime(28619): FATAL EXCEPTION: main
05-31 18:50:24.082: E/AndroidRuntime(28619): java.lang.RuntimeException: Unable to start activity ComponentInfo{yai.properti.tujuh.tujuh.tujuh/yai.properti.tujuh.tujuh.tujuh.WelcomeActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageButton
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.ActivityThread.access$700(ActivityThread.java:140)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.os.Looper.loop(Looper.java:137)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.ActivityThread.main(ActivityThread.java:4921)
05-31 18:50:24.082: E/AndroidRuntime(28619): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 18:50:24.082: E/AndroidRuntime(28619): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 18:50:24.082: E/AndroidRuntime(28619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-31 18:50:24.082: E/AndroidRuntime(28619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-31 18:50:24.082: E/AndroidRuntime(28619): at dalvik.system.NativeStart.main(Native Method)
05-31 18:50:24.082: E/AndroidRuntime(28619): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageButton
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-31 18:50:24.082: E/AndroidRuntime(28619): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-31 18:50:24.082: E/AndroidRuntime(28619): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.Activity.setContentView(Activity.java:1924)
05-31 18:50:24.082: E/AndroidRuntime(28619): at yai.properti.tujuh.tujuh.tujuh.WelcomeActivity.onCreate(WelcomeActivity.java:19)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.Activity.performCreate(Activity.java:5188)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
05-31 18:50:24.082: E/AndroidRuntime(28619): ... 11 more
05-31 18:50:24.082: E/AndroidRuntime(28619): Caused by: java.lang.reflect.InvocationTargetException
05-31 18:50:24.082: E/AndroidRuntime(28619): at java.lang.reflect.Constructor.constructNative(Native Method)
05-31 18:50:24.082: E/AndroidRuntime(28619): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-31 18:50:24.082: E/AndroidRuntime(28619): ... 24 more
05-31 18:50:24.082: E/AndroidRuntime(28619): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010015 a=-1}
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.content.res.Resources.loadDrawable(Resources.java:1925)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.view.View.<init>(View.java:3449)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.widget.ImageView.<init>(ImageView.java:114)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.widget.ImageButton.<init>(ImageButton.java:87)
05-31 18:50:24.082: E/AndroidRuntime(28619): at android.widget.ImageButton.<init>(ImageButton.java:83)
05-31 18:50:24.082: E/AndroidRuntime(28619): ... 27 more
答案 0 :(得分:13)
@Gopal Gopi是对的。如果您已将appcompat v7正确导入到项目中,请在xml中将其用于android:background:
android:background="?android:attr/selectableItemBackground"
此外,请确保您使用此项目的任何项目都有android:clickable="true"
答案 1 :(得分:2)
要使其工作,您应该在App或Activity中使用AppCompact基本主题。如下:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>