具有背景状态的ImageView在多个方向更改后崩溃

时间:2013-12-30 00:24:02

标签: android bitmap imageview android-imageview android-drawable

我有一个带有自定义背景的ImageView,它基本上有两种状态,普通和按下(以及每种状态的图像)。具有背景的imageview以xml定义。通常,一切正常,但经过大量(20+)连续方向更改(横向到纵向等)应用程序崩溃时出现以下错误:

12-30 04:13:38.762: E/AndroidRuntime(29771):    FATAL EXCEPTION: main
12-30 04:13:38.762: E/AndroidRuntime(29771):    java.lang.RuntimeException: Unable to start activity ComponentInfo{MyActivity}: android.view.InflateException: Binary XML file line #39: Error inflating class <unknown
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2314)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3778)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.ActivityThread.access$700(ActivityThread.java:144) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.os.Handler.dispatchMessage(Handler.java:99) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.os.Looper.loop(Looper.java:150) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.ActivityThread.main(ActivityThread.java:5147) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at java.lang.reflect.Method.invokeNative(Native Method) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at java.lang.reflect.Method.invoke(Method.java:511) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at dalvik.system.NativeStart.main(Native Method) 

12-30 04:13:38.762: E/AndroidRuntime(29771):    Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class <unknown
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.createView(LayoutInflater.java:613) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at MyFragment.onCreateView(MyFragment.java:119)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at myapp.myActivity.onStart(MyActivity.java:161)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.Activity.performStart(Activity.java:5148) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
12-30 04:13:38.762: E/AndroidRuntime(29771):    ... 12 more 

12-30 04:13:38.762: E/AndroidRuntime(29771):    Caused by: java.lang.reflect.InvocationTargetException 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at java.lang.reflect.Constructor.constructNative(Native Method) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    ... 30 more 

12-30 04:13:38.762: E/AndroidRuntime(29771):    Caused by: java.lang.OutOfMemoryError 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.content.res.Resources.loadDrawable(Resources.java:2098) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.content.res.Resources.getDrawable(Resources.java:793) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.content.res.Resources.loadDrawable(Resources.java:2083) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.view.View.<init>(View.java:3330) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.widget.ImageView.<init>(ImageView.java:114) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    at android.widget.ImageView.<init>(ImageView.java:110) 
12-30 04:13:38.762: E/AndroidRuntime(29771):    ... 33 more

查看"java.lang.OutOfMemoryError""StateListDrawable.java:173",它似乎与ImageView背景状态有某种关联。 这是怎么回事?我确实在我的片段中保留了对imageview的引用,但我在onDestroyView中将它们设置为null。我可能做错了什么?

PS。没有任何环境保存在任何地方。

3 个答案:

答案 0 :(得分:1)

你可能有内存泄漏。整个片段可能被泄露而不仅仅是ImageView。下载MAT,阅读文章:http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html并找出具体泄漏的内容并从那里开始。

答案 1 :(得分:0)

有两种可能的解决方案,都涉及对XML的更改。与重写所有Java代码相比,这些相对简单。

  1. 简单:锁定屏幕方向:

    <LinearLayout
     android:orientation="portrait">
    
    <--Your stuff-->
    
    </LinearLayout>
    
  2. 不太简单:创建xml的横向版本。为此,请在res目录中创建名为“layout-land”的新文件夹。然后,在该文件夹中创建一个新的xml文件,其拼写与肖像版本相同(例如portrait:blah123.xml,landscape:blah123.xml)。创建您的横向版本。

答案 2 :(得分:0)

回收背景drawable

 if(v.getBackground() != null && 
BitmapDrawable.class.isInstance(v.getBackground())) 
{
((BitmapDrawable)v.getBackground()).getBitmap().recycle();
}