简体   繁体   中英

KitKat specific- android.content.res.Resources$NotFoundException: File res/drawable/list_selector_white.xml from drawable resource ID

I tried the answer here , but it didn't work for me. I am using an xml as background and getting exceptiion - Resources$NotFoundException: File res/drawable/list_selector_white.xml from drawable resource ID . The app is crashing on launch.

The weird part is it is working fine on devices running Lollypop. On OS <= KitKat, this issue is reproing.

XML which is being inflated in fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector_white" >
    ....Other views here....

list_selector_white.xml

<!-- Selector style for listrow -->
    <item android:drawable="@drawable/gradient_bg_white" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="true"/>
    <item android:drawable="@drawable/gradient_bg_hover" android:state_pressed="false" android:state_selected="true"/>

gradient_bg_hover

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <!-- Gradient BgColor for listrow Selected -->
  <gradient
      android:startColor="?attr/listViewRow"
      android:centerColor="?attr/listViewRow"
      android:endColor="?attr/listViewRow"
      android:angle="270" />
</shape>

Logcat:

E/AndroidRuntime( 3879): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
E/AndroidRuntime( 3879):    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
E/AndroidRuntime( 3879):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime( 3879):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
E/AndroidRuntime( 3879):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
E/AndroidRuntime( 3879):    at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
E/AndroidRuntime( 3879):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
E/AndroidRuntime( 3879):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
E/AndroidRuntime( 3879):    at jsoup.LazyAdapterDoubleList.getView(LazyAdapterDoubleList.java:53)
E/AndroidRuntime( 3879):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
E/AndroidRuntime( 3879):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
E/AndroidRuntime( 3879):    at android.widget.ListView.onMeasure(ListView.java:1175)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
E/AndroidRuntime( 3879):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
E/AndroidRuntime( 3879):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:577)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime( 3879):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime( 3879):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime( 3879):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
E/AndroidRuntime( 3879):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime( 3879):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
E/AndroidRuntime( 3879):    at android.view.View.measure(View.java:16497)
E/AndroidRuntime( 3879):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
E/AndroidRuntime( 3879):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
E/AndroidRuntime( 3879):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
E/AndroidRuntime( 3879):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
E/AndroidRuntime( 3879):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
E/AndroidRuntime( 3879):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
E/AndroidRuntime( 3879):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
E/AndroidRuntime( 3879):    at android.view.Choreographer.doFrame(Choreographer.java:544)
E/AndroidRuntime( 3879):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
E/AndroidRuntime( 3879):    at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime( 3879):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 3879):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 3879):    at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime( 3879):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3879):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime( 3879):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime( 3879): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 3879):    at java.lang.reflect.Constructor.newInstance(Native Method)
E/AndroidRuntime( 3879):    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
E/AndroidRuntime( 3879):    ... 50 more
E/AndroidRuntime( 3879): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/list_selector_white.xml from drawable resource ID #0x7f02006a
E/AndroidRuntime( 3879):    at android.content.res.Resources.loadDrawable(Resources.java:2096)
E/AndroidRuntime( 3879):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
E/AndroidRuntime( 3879):    at android.view.View.<init>(View.jav
W/ActivityManager(  589):   Force finishing activity free.programming.programming/.MainActivity
W/ActivityManager(  589): Activity pause timeout for ActivityRecord{655864a8 u0 free.programming.programming/.MainActivity t49 f}

Any lead on how to fix it?

[Edit]
I suspect using attr in drawable is causing issues. Initially, I was using simple @color/... and everything was working fine.

I had the same problem. The problem is that you are using ?attr/ in your drawable. Replace that with a @color or @drawable and it should go away. This does unfortunately mean that you have to bundle a drawable for each of your themes, it is a lot cleaner in lollipop.

Check out this commit of mine on the project I'm working on, it was to resolve exactly this problem.

You could create a drawable-v21 folder and put a drawable under it in hopes of a good time when you can support v21+ but I have chosen to go the other route and bundle different color resources (something you can see in the main branch, I have teal and default green colors).

I was finally able to fix the issue. I will post answer so that it may help future readers.

The problem is that we can't give attr values to item and gradient . I got the issue fixed by taking different xml files for colors.

I ended up using different versions of list_selector_white_1 and _2.xml to:

<!-- Selector style for listrow -->
<item android:drawable="@drawable/gradient_bg_white_1" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/gradient_bg_hover_1" android:state_pressed="true"/>
<item android:drawable="@drawable/gradient_bg_hover_1" android:state_pressed="false" android:state_selected="true"/>

Similarly, I changed gradient_bg_hover and gradient_bg_white to different versions.

Finally, I created an attribute list_selector with format integer and used it in fragment xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="?attr/list_selector" >
    ....Other views here....

I am waiting for the time when I can support API level >= 21 in my app, in order to remove all these hacks.

Basically no, it's a bug that was fixed in Lollipop . The suggestion (until you can minSdkVersion=21 ) is to create a different drawable XML file for each different color you need (and have that as a theme attribute, I assume).

The duplicate question, answers and comments discuss this bug: How to reference colour attribute in drawable?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM