您好我正在尝试创建一个自定义按钮。按钮基本上有两种模式,按下和不按下。我创建了一个名为 addtransaction 的 xml 文件,其中包含两个按钮图像,代码如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/add_transaction_after" android:state_pressed="true"></item>
<item android:drawable="@drawable/add_transaction_before" android:state_focused="false"></item>
</selector>
我面临的问题是,当创建xml文件对应的按钮被分配背景时。出现以下消息
无法解析文件/Users/Rudi/Desktop/ABC/res/drawable/addtransaction.xml
我能够为其他按钮提供自定义,但只有这个 xml 文件显示错误。有人可以告诉我我做错了什么吗?
作为编辑,这里是包含我的按钮的 xml 文件的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="10" >
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="6"
android:background="@drawable/background"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/addAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/account"
android:gravity="center" />
<ImageButton
android:id="@+id/addTransaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/addtransaction"
android:gravity="center" />
<ImageButton
android:id="@+id/viewAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/viewaccount"
android:gravity="center" />
<ImageButton
android:id="@+id/recentTransaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/viewtransaction"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4" >
</LinearLayout>
</LinearLayout>
这是 log cat 显示的错误。
10-26 20:26:02.836: E/ActivityThread(4454): **Failed to inflate
10-26 20:26:02.836: E/ActivityThread(4454): android.view.InflateException: Binary XML file line #28: Error inflating class <unknown>**
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
10-26 20:26:02.836: E/ActivityThread(4454): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-26 20:26:02.836: E/ActivityThread(4454): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.Activity.setContentView(Activity.java:1867)
10-26 20:26:02.836: E/ActivityThread(4454): at com.code.accountmanager.AccountManagerActivity.onCreate(AccountManagerActivity.java:41)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.Activity.performCreate(Activity.java:5008)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2141)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2212)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.ActivityThread.access$600(ActivityThread.java:144)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-26 20:26:02.836: E/ActivityThread(4454): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 20:26:02.836: E/ActivityThread(4454): at android.os.Looper.loop(Looper.java:137)
10-26 20:26:02.836: E/ActivityThread(4454): at android.app.ActivityThread.main(ActivityThread.java:4965)
10-26 20:26:02.836: E/ActivityThread(4454): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 20:26:02.836: E/ActivityThread(4454): at java.lang.reflect.Method.invoke(Method.java:511)
10-26 20:26:02.836: E/ActivityThread(4454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-26 20:26:02.836: E/ActivityThread(4454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-26 20:26:02.836: E/ActivityThread(4454): at dalvik.system.NativeStart.main(Native Method)
10-26 20:26:02.836: E/ActivityThread(4454): Caused by: java.lang.reflect.InvocationTargetException
10-26 20:26:02.836: E/ActivityThread(4454): at java.lang.reflect.Constructor.constructNative(Native Method)
10-26 20:26:02.836: E/ActivityThread(4454): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
10-26 20:26:02.836: E/ActivityThread(4454): ... 25 more
10-26 20:26:02.836: E/ActivityThread(4454): **Caused by: android.content.res.Resources$NotFoundException: File res/drawable/transaction.xml from drawable resource ID #0x7f02000f**
10-26 20:26:02.836: E/ActivityThread(4454): at android.content.res.Resources.loadDrawable(Resources.java:1947)
10-26 20:26:02.836: E/ActivityThread(4454): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
10-26 20:26:02.836: E/ActivityThread(4454): at android.view.View.<init>(View.java:3336)
10-26 20:26:02.836: E/ActivityThread(4454): at android.widget.ImageView.<init>(ImageView.java:114)
10-26 20:26:02.836: E/ActivityThread(4454): at android.widget.ImageButton.<init>(ImageButton.java:87)
10-26 20:26:02.836: E/ActivityThread(4454): at android.widget.ImageButton.<init>(ImageButton.java:83)
10-26 20:26:02.836: E/ActivityThread(4454): ... 28 more
10-26 20:26:02.836: E/ActivityThread(4454): **Caused by: java.lang.NullPointerException
10-26 20:26:02.836: E/ActivityThread(4454): at android.graphics.drawable.DrawableContainer$DrawableContainerState.addChild(DrawableContainer.java:524)**
10-26 20:26:02.836: E/ActivityThread(4454): at android.graphics.drawable.StateListDrawable$StateListState.addStateSet(StateListDrawable.java:278)
10-26 20:26:02.836: E/ActivityThread(4454): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:186)
10-26 20:26:02.836: E/ActivityThread(4454): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
10-26 20:26:02.836: E/ActivityThread(4454): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
10-26 20:26:02.836: E/ActivityThread(4454): at android.content.res.Resources.loadDrawable(Resources.java:1944)
10-26 20:26:02.836: E/ActivityThread(4454): ... 33 more