0

我已经用 5 个标签实现了 tabactivity。当我改变方向和 tabchange 时,我遇到了 outOfMemory 问题。

我试着去

  1. setRetainInstance(true);

  2. 还设置

    @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); }

在我的片段和框架活动中。

  1. 也在android:configChanges="keyboardHidden|orientation|screenLayout|screenSize" 清单中添加。

但没有运气。我在下面添加了我的 logcat,

09-03 10:34:30.475: E/dalvikvm-heap(8691): Out of memory on a 6554896-byte allocation.
09-03 10:34:30.475: I/dalvikvm(25175): "main" prio=5 tid=1 RUNNABLE
09-03 10:34:30.475: I/dalvikvm(25175):   | group="main" sCount=0 dsCount=0 obj=0x41127508 self=0x410c5b78
09-03 10:34:30.475: I/dalvikvm(25175):   | sysTid=25175 nice=0 sched=0/0 cgrp=apps handle=1074904880
09-03 10:34:30.475: I/dalvikvm(25175):   | schedstat=( 7353555166 1878419412 11747 ) utm=577 stm=157 core=3
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.content.res.Resources.loadDrawable(Resources.java:1963)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.View.<init>(View.java:3444)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.View.<init>(View.java:3377)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.ViewGroup.<init>(ViewGroup.java:426)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.widget.RelativeLayout.<init>(RelativeLayout.java:184)
09-03 10:34:30.475: I/dalvikvm(25175):   at java.lang.reflect.Constructor.constructNative(Native Method)
09-03 10:34:30.475: I/dalvikvm(25175):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-03 10:34:30.475: I/dalvikvm(25175):   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
09-03 10:34:30.475: I/dalvikvm(25175):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-03 10:34:30.480: I/dalvikvm(25175):   at com.dameco.Settings.onCreateView(Settings.java:113)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.Activity.performStart(Activity.java:5216)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.ActivityThread.startActivityNow(ActivityThread.java:1941)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:705)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabHost.setCurrentTab(TabHost.java:369)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:560)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.View.performClick(View.java:4223)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.view.View$PerformClick.run(View.java:17275)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.os.Handler.handleCallback(Handler.java:615)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.os.Looper.loop(Looper.java:137)
09-03 10:34:30.480: I/dalvikvm(25175):   at android.app.ActivityThread.main(ActivityThread.java:4898)
09-03 10:34:30.480: I/dalvikvm(25175):   at java.lang.reflect.Method.invokeNative(Native Method)
09-03 10:34:30.480: I/dalvikvm(25175):   at java.lang.reflect.Method.invoke(Method.java:511)
09-03 10:34:30.480: I/dalvikvm(25175):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-03 10:34:30.480: I/dalvikvm(25175):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-03 10:34:30.480: I/dalvikvm(25175):   at dalvik.system.NativeStart.main(Native Method)

请就此给我建议。谢谢。

4

2 回答 2

0

从日志看来,视图正在尝试从资源重新创建位图,但由于 OOM 而失败。

位图不会立即被垃圾收集。这种情况下的 GC 周期(Native Heap)以更大的延迟/间隔运行。在更改方向时尝试回收位图或尝试保留位图。

不确定您在做什么,发布更多代码肯定会有所帮助。

也不要使用configChanges它是一种不好的做法。指定此项会告诉活动管理器,您将处理与配置更改相关的所有事情,系统不会为您做任何事情。

来自SDSO 回答

setRetainInstance(true):建议与不包含任何引用的片段一起使用,它将在旋转时重新创建。这意味着您不应该在任何包含上下文、视图等的片段上使用它。典型的视觉片段可以。但是对于保存对象的片段非常有用,例如运行线程、异步任务、数据集合、加载的资产、获取的结果等。此方法有助于将非可视片段用作可拆卸的持有者,用于 Activity 的非上下文相关对象.

于 2013-09-03T06:13:28.623 回答
0

删除这一行,因为你已经在清单中给出了相关的

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } 
于 2013-09-03T06:09:32.293 回答