Android Custom Button State Selector XML file caus

2019-02-24 23:39发布

问题:

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

回答1:

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.