我尝试了答案here,但它对我不起作用。我使用xml作为背景并获得例外 - Resources$NotFoundException: File res/drawable/list_selector_white.xml from drawable resource ID
。该应用程序在发布时崩溃。
奇怪的是它在运行Lollypop的设备上运行良好。在OS< = KitKat上,这个问题正在重新开始。
在片段中膨胀的XML:
<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}
如何解决这个问题?
[编辑]
我怀疑在drawable中使用attr
会导致问题。最初,我使用的是简单@color/...
,一切正常。
答案 0 :(得分:9)
我遇到了同样的问题。
问题是您在drawable中使用?attr/
。
将其替换为@color
或@drawable
,它应该消失。
不幸的是,这意味着您必须为每个主题捆绑一个drawable,它在棒棒糖中更加清晰。
在我正在进行的项目中查看我的this commit,就是要解决这个问题。
您可以创建一个drawable-v21
文件夹并在其下放置一个drawable,希望能够支持v21 +,但我选择了另一条路径并捆绑不同的颜色资源(您可以在主要分支,我有蓝绿色和默认绿色)。
答案 1 :(得分:5)
我终于能够解决这个问题了。我会发布答案,以便它可以帮助未来的读者。
问题在于我们无法将{at}值提供给item
和gradient
。我通过为颜色采用不同的xml文件来解决问题。
我最终使用不同版本的list_selector_white_1和_2.xml来:
<!-- 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"/>
同样,我将gradient_bg_hover和gradient_bg_white更改为不同的版本。
最后,我创建了一个格式为integer
的属性list_selector,并在片段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....
我正在等待我可以在我的应用中支持API级别&gt; = 21的时间,以便删除所有这些黑客。
答案 2 :(得分:4)
基本上不,它是bug that was fixed in Lollipop。建议(直到你可以minSdkVersion=21
)是为你需要的每种不同颜色创建一个不同的可绘制XML文件(并假设它作为主题属性)。
重复的问题,答案和评论讨论了这个错误:How to reference colour attribute in drawable?