Android 自定义按钮状态选择器 XML 文件导致 Resource$NotFoundException
由于 Resource$NotFoundException,我的活动随机崩溃,并且总是由于我的自定义按钮的状态选择器文件之一而导致。而且崩溃是完全随机的,我将多次检查整个应用程序而不会发生崩溃,然后第 n 次转到 Activity,然后它会抛出异常。
堆栈跟踪:
07-12 11:12:18.469 2086 2086 D AndroidRuntime: Shutting down VM
07-12 11:12:18.469 2086 2086 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x400208b0)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: FATAL EXCEPTION: main
07-12 11:12:18.508 2086 2086 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appname.droid/com.appname.droid.send.ConfirmActivity}: android.view.InflateException: Binary XML file line #123: Error inflating class <unknown>
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.access$2500(ActivityThread.java:129)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.os.Looper.loop(Looper.java:143)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4701)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #123: Error inflating class <unknown>
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:513)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:203)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.Activity.setContentView(Activity.java:1647)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.appname.droid.send.ConfirmActivity.onCreate(ConfirmActivity.java:40)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 11 more
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.widget.Button.<init>(Button.java:65)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Constructor.constructNative(Native Method)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:500)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 24 more
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_green.xml from drawable resource ID #0x7f020020
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.content.res.Resources.loadDrawable(Resources.java:1725)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.View.<init>(View.java:1885)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.widget.TextView.<init>(TextView.java:369)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.widget.Button.<init>(Button.java:69)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 28 more
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: java.lang.NullPointerException
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.DrawableContainer$DrawableContainerState.addChild(DrawableContainer.java:349)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.StateListDrawable$StateListState.addStateSet(StateListDrawable.java:265)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:796)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.Drawable.createFromXml(Drawable.java:737)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.content.res.Resources.loadDrawable(Resources.java:1722)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 32 more
07-12 11:12:18.515 1235 1235 W ActivityManager: Force finishing activity com.appname.droid/.send.ConfirmActivity
ConfirmActivity 中的第 40 行是一段代码:
setContentView(R.layout.confirm);
这里是 res/drawable/btn_green.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/green_large_btn_pressed" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/green_large_btn_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/green_large_btn" />
<item
android:state_enabled="true"
android:drawable="@drawable/green_large_btn" />
</selector>
此异常发生在整个应用程序中,它始终是我的按钮之一。我的 btn_gray 和 btn_orange 也发生同样的情况。
color_large_btn.png 和 color_large_btn_pressed.png 对于所有颜色都存在。
安卓版本: 目前在运行 Android 2.2 的 Droid2 上 运行 Android 2.1 的 Motorola Blur 也会发生这种情况 在运行 Android 2.3 的 HTC Evo 上似乎不会发生这种情况
My Activities are randomly crashing due to Resource$NotFoundException and it's always due to one of my custom button's state selector files. And the crashes are completely random, I'll go through the entire app multiple times with no crashes, and then go to an Activity for the nth time, and then it'll throw the exception.
Stack Trace:
07-12 11:12:18.469 2086 2086 D AndroidRuntime: Shutting down VM
07-12 11:12:18.469 2086 2086 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x400208b0)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: FATAL EXCEPTION: main
07-12 11:12:18.508 2086 2086 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appname.droid/com.appname.droid.send.ConfirmActivity}: android.view.InflateException: Binary XML file line #123: Error inflating class <unknown>
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.access$2500(ActivityThread.java:129)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.os.Looper.loop(Looper.java:143)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4701)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #123: Error inflating class <unknown>
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:513)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:203)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.Activity.setContentView(Activity.java:1647)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at com.appname.droid.send.ConfirmActivity.onCreate(ConfirmActivity.java:40)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 11 more
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.widget.Button.<init>(Button.java:65)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Constructor.constructNative(Native Method)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:500)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 24 more
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_green.xml from drawable resource ID #0x7f020020
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.content.res.Resources.loadDrawable(Resources.java:1725)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.view.View.<init>(View.java:1885)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.widget.TextView.<init>(TextView.java:369)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.widget.Button.<init>(Button.java:69)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 28 more
07-12 11:12:18.508 2086 2086 E AndroidRuntime: Caused by: java.lang.NullPointerException
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.DrawableContainer$DrawableContainerState.addChild(DrawableContainer.java:349)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.StateListDrawable$StateListState.addStateSet(StateListDrawable.java:265)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:796)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.graphics.drawable.Drawable.createFromXml(Drawable.java:737)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: at android.content.res.Resources.loadDrawable(Resources.java:1722)
07-12 11:12:18.508 2086 2086 E AndroidRuntime: ... 32 more
07-12 11:12:18.515 1235 1235 W ActivityManager: Force finishing activity com.appname.droid/.send.ConfirmActivity
And line 40 in ConfirmActivity is the piece of code:
setContentView(R.layout.confirm);
And here is res/drawable/btn_green.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/green_large_btn_pressed" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/green_large_btn_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/green_large_btn" />
<item
android:state_enabled="true"
android:drawable="@drawable/green_large_btn" />
</selector>
This exception occurs throughout the app and it's always one of my buttons. The same thing happens with my btn_gray and btn_orange.
color_large_btn.png and color_large_btn_pressed.png are all present for all colors.
Android Version:
Currently on a Droid2 running Android 2.2
Also happens on a Motorola Blur running Android 2.1
Doesn't seem to happen on HTC Evo running Android 2.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题本质上是内存不足的情况。在将 xml 文件封装到 try-catch 块中之后,我能够克服无法加载文件/找到它们的初始错误。结果是活动的图像完全丢失,直到最终抛出实际的内存不足异常。
使固定?发现并删除了许多泄漏并缩小了几个图像文件的大小。
This issue is essentially an out of memory scenario. After wrapping up my inflating of xml files in try-catch blocks, I was able to get passed this initial error of not being able to load in files/ find them. The resulting behavior is Activities with images just plain missing until finally an actual out of memory exception is thrown.
Fix? Found and removed numerous leaks and down sized a couple image files.