检查我的应用程序的MOCKUP:
问题:
如果我点击第一页上的第二页按钮,它会打开第二页的新意图
然后
我点击SECOND PAGE中的 THIRD PAGE 按钮,然后转到第三页
然后
我再次使用FIRST PAGE BUTTON 然后崩溃应用程序进入 FIRST PAGE ,给出以下错误“ 内存不足.... “
错误:
05-13 11:35:06.417: D/dalvikvm(11948): GC_BEFORE_OOM freed 9K, 19% free 39660K/48583K, paused 36ms, total 36ms
05-13 11:35:06.417: E/dalvikvm-heap(11948): Out of memory on a 8684816-byte allocation.
05-13 11:35:06.425: I/dalvikvm(11948): "main" prio=5 tid=1 RUNNABLE
05-13 11:35:06.425: I/dalvikvm(11948): | group="main" sCount=0 dsCount=0 obj=0x40f01508 self=0x40d41a18
05-13 11:35:06.425: I/dalvikvm(11948): | sysTid=11948 nice=0 sched=0/0 cgrp=apps handle=1074921264
05-13 11:35:06.425: I/dalvikvm(11948): | schedstat=( 5318603531 1290588360 5684 ) utm=446 stm=85 core=0
05-13 11:35:06.425: I/dalvikvm(11948): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-13 11:35:06.425: I/dalvikvm(11948): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:592)
05-13 11:35:06.425: I/dalvikvm(11948): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
05-13 11:35:06.425: I/dalvikvm(11948): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
05-13 11:35:06.425: I/dalvikvm(11948): at android.content.res.Resources.loadDrawable(Resources.java:1963)
05-13 11:35:06.425: I/dalvikvm(11948): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-13 11:35:06.425: I/dalvikvm(11948): at android.view.View.<init>(View.java:3443)
05-13 11:35:06.425: I/dalvikvm(11948): at android.view.View.<init>(View.java:3376)
05-13 11:35:06.425: I/dalvikvm(11948): at android.view.ViewGroup.<init>(ViewGroup.java:426)
05-13 11:35:06.425: I/dalvikvm(11948): at android.widget.RelativeLayout.<init>(RelativeLayout.java:184)
05-13 11:35:06.425: I/dalvikvm(11948): at java.lang.reflect.Constructor.constructNative(Native Method)
05-13 11:35:06.425: I/dalvikvm(11948): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-13 11:35:06.433: I/dalvikvm(11948): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-13 11:35:06.433: I/dalvikvm(11948): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-13 11:35:06.433: I/dalvikvm(11948): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
05-13 11:35:06.433: I/dalvikvm(11948): at android.app.Activity.setContentView(Activity.java:1912)
05-13 11:35:06.440: I/dalvikvm(11948): at com.mca.LastingPowerActivity.onCreate(LastingPowerActivity.java:24)
05-13 11:35:06.440: I/dalvikvm(11948): at android.app.Activity.performCreate(Activity.java:5163)
05-13 11:35:06.440: I/dalvikvm(11948): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-13 11:35:06.440: I/dalvikvm(11948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
05-13 11:35:06.440: I/dalvikvm(11948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
05-13 11:35:06.440: I/dalvikvm(11948): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-13 11:35:06.440: I/dalvikvm(11948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
05-13 11:35:06.440: I/dalvikvm(11948): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 11:35:06.440: I/dalvikvm(11948): at android.os.Looper.loop(Looper.java:137)
05-13 11:35:06.440: I/dalvikvm(11948): at android.app.ActivityThread.main(ActivityThread.java:4895)
05-13 11:35:06.440: I/dalvikvm(11948): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 11:35:06.440: I/dalvikvm(11948): at java.lang.reflect.Method.invoke(Method.java:511)
05-13 11:35:06.440: I/dalvikvm(11948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-13 11:35:06.440: I/dalvikvm(11948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-13 11:35:06.440: I/dalvikvm(11948): at dalvik.system.NativeStart.main(Native Method)
05-13 11:35:06.440: D/skia(11948): --- decoder->decode returned false
05-13 11:35:06.440: D/AndroidRuntime(11948): Shutting down VM
05-13 11:35:06.440: W/dalvikvm(11948): threadid=1: thread exiting with uncaught exception (group=0x40f002a0)
05-13 11:35:06.456: E/AndroidRuntime(11948): FATAL EXCEPTION: main
05-13 11:35:06.456: E/AndroidRuntime(11948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mca/com.mca.LastingPowerActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.os.Looper.loop(Looper.java:137)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.ActivityThread.main(ActivityThread.java:4895)
05-13 11:35:06.456: E/AndroidRuntime(11948): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 11:35:06.456: E/AndroidRuntime(11948): at java.lang.reflect.Method.invoke(Method.java:511)
05-13 11:35:06.456: E/AndroidRuntime(11948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-13 11:35:06.456: E/AndroidRuntime(11948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-13 11:35:06.456: E/AndroidRuntime(11948): at dalvik.system.NativeStart.main(Native Method)
05-13 11:35:06.456: E/AndroidRuntime(11948): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-13 11:35:06.456: E/AndroidRuntime(11948): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-13 11:35:06.456: E/AndroidRuntime(11948): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.Activity.setContentView(Activity.java:1912)
05-13 11:35:06.456: E/AndroidRuntime(11948): at com.mca.LastingPowerActivity.onCreate(LastingPowerActivity.java:24)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.Activity.performCreate(Activity.java:5163)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
05-13 11:35:06.456: E/AndroidRuntime(11948): ... 11 more
05-13 11:35:06.456: E/AndroidRuntime(11948): Caused by: java.lang.reflect.InvocationTargetException
05-13 11:35:06.456: E/AndroidRuntime(11948): at java.lang.reflect.Constructor.constructNative(Native Method)
05-13 11:35:06.456: E/AndroidRuntime(11948): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-13 11:35:06.456: E/AndroidRuntime(11948): ... 26 more
05-13 11:35:06.456: E/AndroidRuntime(11948): Caused by: java.lang.OutOfMemoryError
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:592)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.content.res.Resources.loadDrawable(Resources.java:1963)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.View.<init>(View.java:3443)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.View.<init>(View.java:3376)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.view.ViewGroup.<init>(ViewGroup.java:426)
05-13 11:35:06.456: E/AndroidRuntime(11948): at android.widget.RelativeLayout.<init>(RelativeLayout.java:184)
05-13 11:35:06.456: E/AndroidRuntime(11948): ... 29 more
如果有人知道然后分享你的答案,那么我就可以解决它。
谢谢。
答案 0 :(得分:0)
确保您重复使用应用程序堆栈中的活动,而不是每次都创建活动的新实例。如果不是这样的话那么应用程序的堆栈肯定会耗尽内存。
答案 1 :(得分:0)
我也面临同样的问题,我正在做的是跟随。
当您的活动离开其状态并转到其他活动时,它会在成功启动 activity2 后调用“onStop()”。这样做
@Override
protected void onStop()
{
relativeLayout.setBackgroundDrawable(null);
}
当你关闭 activity2 并返回 activity1 时,“onResume()会调用。所以这样做,
@Override
protected void onResume()
{
relativeLayout.setBackgroundResource(R.drawable.home_bg);
}
这将解决您的问题。
答案 2 :(得分:0)
您必须卸载纹理图集并在需要时重新加载它们,否则肯定会产生内存不足错误。
以下是如何卸载纹理以释放内存:
// all the textures you think are big
Engine.getTextureManager().unloadTexture(bg);
bg.clearTextureAtlasSources();
System.gc();
我希望这有助于解决您的问题。
答案 3 :(得分:0)
我想首先提一下两个解决方案,如果您使用的是不太重的图片,请在AndroidManifest.xml中添加android:largeHeap="true"
。
或强>
使用Outofmemory solution链接加载您的位图,并在切换活动时回收该位图。它的常见问题及其解决方案多次在stackoverflow中提供。
如果您查看现有问题的答案,看看您的问题是否与现有问题相匹配,那么最好的情况可能是最好的,如果不匹配,请写出使您的情况不同的原因。
Strange out of memory issue while loading an image to a Bitmap object
Out of memory exception due to large bitmap size
Android handling out of memory exception on image processing
答案 4 :(得分:0)
将堆大小保持在最低限度;
当您从第3页返回时,您可以使用下面的代码清除活动。
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//to clear previous activities
startActivity(intent);
或者您可以用来清除活动。
finish();
或者您只需传递上下文而不是创建新的
Activity a = (Activity) getContext();
或者您可以使用fragments。