1

检查我的应用程序的这个模型: 小样


问题 :

如果我从第一页点击第二页按钮,它会打开第二页的新意图

然后

我从第二页单击第三页按钮,然后转到第三页

然后

我再次使用第一页按钮进入第一页,然后通过给出以下错误“内存不足... ”使应用程序崩溃


错误 :

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

如果有人知道,请分享您的答案,以便我解决

谢谢你。

4

5 回答 5

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();

或者你可以使用片段

于 2014-05-12T11:06:10.200 回答
0

我也面临同样的问题,我正在做的事情是跟随。

当您的活动离开其状态并转到其他活动时,它会在成功启动活动2 后调用“onStop()”。所以这样做

@Override
protected void onStop() 
{
    relativeLayout.setBackgroundDrawable(null);
}

当您关闭activity2并返回activity1时,“onResume()将调用。所以这样做,

@Override
protected void onResume() 
{
    relativeLayout.setBackgroundResource(R.drawable.home_bg);
}

这将解决您的问题。

于 2013-05-14T07:21:48.280 回答
0

如果需要,您必须卸载纹理图集并重新加载它们,否则肯定会产生内存不足错误。
以下是如何卸载纹理以释放内存:

    // all the textures you think are big      
    Engine.getTextureManager().unloadTexture(bg); 
    bg.clearTextureAtlasSources();    
    System.gc();

我希望它能帮助解决您的问题。

于 2013-05-14T07:50:00.137 回答
0

确保您正在重用应用程序堆栈中的活动,而不是每次都创建一个新的活动实例。如果不是这种情况,那么应用程序的堆栈肯定会耗尽内存。

于 2013-05-14T06:55:43.567 回答
0

android:largeHeap="true"如果您在 AndroidManifest.xml 中添加的图片不多,我想先提两个解决方案 。

或者

使用Outofmemory 解决方案链接加载您的位图,并在切换活动时回收该位图。它的常见问题及其解决方案多次在stackoverflow中提供。

如果您查看现有问题的答案,看看您的答案是否与现有的一些答案相匹配,那可能是最好的,如果不匹配,请写出使您的情况不同的原因。

将图像加载到 Bitmap 对象时出现奇怪的内存不足问题

由于大位图大小导致内存不足异常

Android处理图像处理内存不足异常

于 2013-05-14T09:00:32.010 回答