android - When set the background with a selector,it cause resouce not found -
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <relativelayout android:id="@+id/rellay_action_bar_bg" android:layout_width="match_parent" android:layout_height="@dimen/action_bar_height" android:background="@color/blue" > <button android:id="@+id/btn_action_bar_left" android:layout_width="20dp" android:layout_height="30dp" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_marginleft="5dp" android:background="@drawable/selector_btn_back" android:contentdescription="@string/app_name" android:visibility="invisible" /> <textview android:id="@+id/tv_action_bar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:layout_gravity="center" android:textcolor="@color/white" android:textsize="@dimen/text_size" /> <button android:id="@+id/btn_action_bar_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="5dp" android:background="@drawable/selector_btn_action_bar_right" android:contentdescription="@string/app_name" android:visibility="invisible" /> </relativelayout> <linearlayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/rellay_action_bar_bg" android:orientation="vertical" > </linearlayout> </relativelayout>
this layout cause problem."android:background="@drawable/selector_btn_action_bar_right"
" view.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/blue" android:state_pressed="true"></item> <item android:color="@color/white" android:state_pressed="false"></item> </selector>
and selector
04-26 01:30:01.550: e/androidruntime(1556): fatal exception: main 04-26 01:30:01.550: e/androidruntime(1556): java.lang.runtimeexception: unable start activity componentinfo{com.yuanpaiapp.yuanpai.android/com.yuanpaiapp.yuanpai.android.activity.loginactivity}: android.view.inflateexception: binary xml file line #32: error inflating class 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activitythread.access$600(activitythread.java:141) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 04-26 01:30:01.550: e/androidruntime(1556): @ android.os.handler.dispatchmessage(handler.java:99) 04-26 01:30:01.550: e/androidruntime(1556): @ android.os.looper.loop(looper.java:137) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activitythread.main(activitythread.java:5041) 04-26 01:30:01.550: e/androidruntime(1556): @ java.lang.reflect.method.invokenative(native method) 04-26 01:30:01.550: e/androidruntime(1556): @ java.lang.reflect.method.invoke(method.java:511) 04-26 01:30:01.550: e/androidruntime(1556): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 04-26 01:30:01.550: e/androidruntime(1556): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 04-26 01:30:01.550: e/androidruntime(1556): @ dalvik.system.nativestart.main(native method) 04-26 01:30:01.550: e/androidruntime(1556): caused by: android.view.inflateexception: binary xml file line #32: error inflating class 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.createview(layoutinflater.java:613) 04-26 01:30:01.550: e/androidruntime(1556): @ com.android.internal.policy.impl.phonelayoutinflater.oncreateview(phonelayoutinflater.java:56) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.oncreateview(layoutinflater.java:660) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:685) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.rinflate(layoutinflater.java:746) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.rinflate(layoutinflater.java:749) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.inflate(layoutinflater.java:489) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.inflate(layoutinflater.java:396) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.inflate(layoutinflater.java:352) 04-26 01:30:01.550: e/androidruntime(1556): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:270) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activity.setcontentview(activity.java:1881) 04-26 01:30:01.550: e/androidruntime(1556): @ com.yuanpaiapp.yuanpai.android.baseactivity.oncreate(baseactivity.java:29) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activity.performcreate(activity.java:5104) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 04-26 01:30:01.550: e/androidruntime(1556): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 04-26 01:30:01.550: e/androidruntime(1556): ... 11 more 04-26 01:30:01.550: e/androidruntime(1556): caused by: java.lang.reflect.invocationtargetexception 04-26 01:30:01.550: e/androidruntime(1556): @ java.lang.reflect.constructor.constructnative(native method) 04-26 01:30:01.550: e/androidruntime(1556): @ java.lang.reflect.constructor.newinstance(constructor.java:417) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.layoutinflater.createview(layoutinflater.java:587) 04-26 01:30:01.550: e/androidruntime(1556): ... 25 more 04-26 01:30:01.550: e/androidruntime(1556): caused by: android.content.res.resources$notfoundexception: file res/drawable-mdpi-v4/selector_btn_action_bar_right.xml drawable resource id #0x7f020019 04-26 01:30:01.550: e/androidruntime(1556): @ android.content.res.resources.loaddrawable(resources.java:1953) 04-26 01:30:01.550: e/androidruntime(1556): @ android.content.res.typedarray.getdrawable(typedarray.java:601) 04-26 01:30:01.550: e/androidruntime(1556): @ android.view.view.(view.java:3330) 04-26 01:30:01.550: e/androidruntime(1556): @ android.widget.textview.(textview.java:583) 04-26 01:30:01.550: e/androidruntime(1556): @ android.widget.button.(button.java:107) 04-26 01:30:01.550: e/androidruntime(1556): @ android.widget.button.(button.java:103) 04-26 01:30:01.550: e/androidruntime(1556): ... 28 more 04-26 01:30:01.550: e/androidruntime(1556): caused by: org.xmlpull.v1.xmlpullparserexception: binary xml file line #4: tag requires 'drawable' attribute or child tag defining drawable 04-26 01:30:01.550: e/androidruntime(1556): @ android.graphics.drawable.statelistdrawable.inflate(statelistdrawable.java:178) 04-26 01:30:01.550: e/androidruntime(1556): @ android.graphics.drawable.drawable.createfromxmlinner(drawable.java:885) 04-26 01:30:01.550: e/androidruntime(1556): @ android.graphics.drawable.drawable.createfromxml(drawable.java:822) 04-26 01:30:01.550: e/androidruntime(1556): @ android.content.res.resources.loaddrawable(resources.java:1950) 04-26 01:30:01.550: e/androidruntime(1556): ... 33 more
use drawable instead of color .
there 2 optional methods,
using shape in selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@color/blue" /> </shape> </item> <item > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@color/white" /> </shape> </item> </selector>
using picture in selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/blue" android:state_pressed="true" /> <item android:drawable="@drawable/white" android:state_pressed="false" /> </selector>
Comments
Post a Comment