此代码在Nexus 5 / Lollipop 5.1上运行良好,但当我尝试在糟糕的LG E615f / ICS 4.0.4上运行时,这个奇怪的错误开始发生。
05-19 18:34:07.639 13037-13037/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #63: Error inflating class android.widget.ImageButton
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.myclass.adapters.TasksAdapter.getView(TasksAdapter.java:66)
at android.widget.AbsListView.obtainView(AbsListView.java:2466)
at android.widget.ListView.makeAndAddView(ListView.java:1772)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillFromTop(ListView.java:732)
at android.widget.ListView.layoutChildren(ListView.java:1625)
at android.widget.AbsListView.onLayout(AbsListView.java:2296)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1493)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:586)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.myclass.adapters.TasksAdapter.getView(TasksAdapter.java:66)
at android.widget.AbsListView.obtainView(AbsListView.java:2466)
at android.widget.ListView.makeAndAddView(ListView.java:1772)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillFromTop(ListView.java:732)
at android.widget.ListView.layoutChildren(ListView.java:1625)
at android.widget.AbsListView.onLayout(AbsListView.java:2296)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11345)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1493)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ripple.xml from drawable resource ID #0x7f0200b
这是抱怨的ImageButton
:
<ImageButton
android:id="@+id/taskItemDownloadBtn"
android:layout_width="@dimen/listItemIconSize"
android:layout_height="@dimen/listItemIconSize"
android:layout_gravity="center"
android:src="@drawable/ic_cloud"
android:background="@drawable/ripple"
android:contentDescription="@null" />
ripple.xml
的两个版本没有找到:
drawable/ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="?colorControlHighlight" />
</shape>
</item>
</selector>
drawable-v21/ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?colorControlHighlight"/>
我试图:
ImageButton
更改为ImageView
和Button
没有成功; 答案 0 :(得分:1)
问题是由ResourcesNotFoundException
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ripple.xml from drawable resource ID #0x7f0200b
尝试在ripple.xml
文件夹中添加文件/drawable
。
* -
如果您已经拥有此资源,只需刷新项目并重新构建,以确保将资源ID #0x7f0200b
添加到R.java
文件中。
问题是该属性:
android:color="?colorControlHighlight"
在API级别21 中添加了
答案 1 :(得分:0)
发现问题。它实际上是drawable/ripple.xml
。当我删除它时,其他可绘制的XML中出现同样问题的错误。
似乎较旧的Androids无法解释属性引用,例如:?colorAccent
和?attr:colorControlHighlight
,在可绘制的XML中,仅在布局XML中。不得不回到那里直接参考颜色:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="@color/makara" />
<!-- `?colorControlHighlight` does not work here -->
</shape>
</item>
</selector>
感谢那位提出此建议的人并稍后删除了答案!