无法将选择器设置为RadioButton的背景

时间:2013-12-09 08:19:47

标签: android radio-button drawable

当我尝试将选择器设置为RadioButton的背景时,我遇到了一个奇怪的错误 我的布局很简单:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rbtnPhoto"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/icon_photo_selector" />

    <RadioButton
        android:id="@+id/rbtnTagline"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/icon_tagline_selector" />

    <RadioButton
        android:id="@+id/rbtnTranslator"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/icon_translator_selector"/>

    <RadioButton
        android:id="@+id/rbtnDiscount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/icon_discount_selector" />
</RadioGroup>

我的选择器是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/icon_photo_selected" android:state_checked="true"></item>
    <item android:drawable="@drawable/icon_photo_unselected" android:state_checked="false"></item>

</selector>

上面的drawables是九个路径图像。我总是得到“android.view.InflateException:Binary XML file line”的异常。但如果我用图像更改选择器,布局可能会膨胀 请帮我解决。
更新
我的图像是很好的补丁图像
这是我的日志:

    12-09 15:36:07.256: E/AndroidRuntime(15211): FATAL EXCEPTION: main
12-09 15:36:07.256: E/AndroidRuntime(15211): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paktor/com.paktor.MainActivity}: android.view.InflateException: Binary XML file line #39: Error inflating class <unknown>
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.os.Looper.loop(Looper.java:137)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.ActivityThread.main(ActivityThread.java:4482)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at java.lang.reflect.Method.invoke(Method.java:511)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at dalvik.system.NativeStart.main(Native Method)
12-09 15:36:07.256: E/AndroidRuntime(15211): Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class <unknown>
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at com.paktor.fragments.HomeVer3Fragment.onCreateView(HomeVer3Fragment.java:23)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at com.paktor.MainActivity.onStart(MainActivity.java:91)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1135)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.Activity.performStart(Activity.java:4740)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1933)
12-09 15:36:07.256: E/AndroidRuntime(15211):    ... 11 more
12-09 15:36:07.256: E/AndroidRuntime(15211): Caused by: java.lang.reflect.InvocationTargetException
12-09 15:36:07.256: E/AndroidRuntime(15211):    at java.lang.reflect.Constructor.constructNative(Native Method)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
12-09 15:36:07.256: E/AndroidRuntime(15211):    ... 30 more
12-09 15:36:07.256: E/AndroidRuntime(15211): Caused by: java.lang.StackOverflowError
12-09 15:36:07.256: E/AndroidRuntime(15211):    at java.lang.ref.FinalizerReference.add(FinalizerReference.java:48)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.XmlBlock$Parser.<init>(XmlBlock.java:78)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.XmlBlock.newParser(XmlBlock.java:71)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2128)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.Resources.loadDrawable(Resources.java:1918)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.Resources.getDrawable(Resources.java:664)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.Resources.loadDrawable(Resources.java:1920)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.Resources.getDrawable(Resources.java:664)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.Resources.loadDrawable(Resources.java:1920)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.content.res.Resources.getDrawable(Resources.java:664)
12-09 15:36:07.256: E/AndroidRuntime(15211):    at android.graphics.drawable.

2 个答案:

答案 0 :(得分:0)

您忘记了<selector> - 代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/icon_photo_selected" android:state_checked="true" />
<item android:drawable="@drawable/icon_photo_unselected" android:state_checked="false" />
</selector>

我认为这应该可以解决您的问题

答案 1 :(得分:0)

将您的选择器更改为:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
<item android:drawable="@drawable/icon_photo_selected" android:state_checked="true"></item> 
<item android:drawable="@drawable/icon_photo_unselected" android:state_checked="false"></item> 
</selector>

选择器标签似乎缺失