设置图像按钮的背景时出现分析错误

时间:2012-10-25 18:14:07

标签: android android-layout

您好我正在尝试创建自定义按钮。按钮基本上有两种模式,按下和未按下。我创建了一个名为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

3 个答案:

答案 0 :(得分:1)

尝试添加:

<item android:drawable="@drawable/some_drawable" />

如果这样做无效,请尝试以下步骤:

  1. 删除项目的库声明。
  2. 以图形布局打开导致错误的布局(使用选择器的布局)。
  3. 清理项目(项目 - &gt;清洁)
  4. 关闭并重新开启日食。 (现在布局应该没问题)
  5. 您可以再次将项目设置为库。
  6. Here's original answer.

答案 1 :(得分:0)

您说您的可绘制xml名为addtransaction.xml - 在您的布局文件中,您将其引用为@drawable/trans -

因此,将该行更改为android:background="@drawable/addtransaction",您应该完成。

答案 2 :(得分:0)

事实证明,图像本身存在一些错误,再次制作图像,将其用作按钮的背景并且没有解析错误。