gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:11:47 24 4
gpt4 key购买 nike

我有一个带有自定义背景的 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 背景状态有关。这是怎么回事?我确实在我的 fragment 中保留了对 imageview 的引用,但我在 onDestroyView 中将它们设置为 null。我可能做错了什么?

附言。没有上下文保存在任何地方。

最佳答案

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

关于android - 多个方向更改后,具有背景状态的 ImageView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20831690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com