我有一个Xamarin.Android应用程序,在她的主要游戏活动(25岁以上)中包含很多按钮。
当我按下按钮时,我的应用程序随机崩溃。我有以下崩溃日志:
Xamarin caused by: java.lang.OutOfMemoryError: Failed to allocate a 44236812 byte allocation with 8344256 free bytes and 7MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)
at android.content.res.Resources.loadDrawable(Resources.java:4085)
at android.content.res.Resources.getDrawable(Resources.java:2005)
at android.content.res.Resources.getDrawable(Resources.java:1987)
at android.content.Context.getDrawable(Context.java:464)
at android.view.View.setBackgroundResource(View.java:18532)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我不知道真正发生了什么以及如何解决。一个有趣的事实是,当我安装该应用程序时,我第一次使用它时没有遇到任何问题。如果我关闭应用程序并重新启动它,则会在按钮单击事件中随机崩溃。按钮操作是在ImageView上设置不同的背景图像。
您可以找到更改背景的我的方法的快照:
private void SetErrorImage(ImageView component, int errorNumber)
{
switch (errorNumber)
{
case 0:
component.SetBackgroundResource(Resource.Drawable.arbre_00);
break;
case 1:
component.SetBackgroundResource(Resource.Drawable.arbre_01);
break;
case 2:
component.SetBackgroundResource(Resource.Drawable.arbre_02);
break;
case 3:
component.SetBackgroundResource(Resource.Drawable.arbre_03);
break;
case 4:
component.SetBackgroundResource(Resource.Drawable.arbre_04);
break;
case 5:
component.SetBackgroundResource(Resource.Drawable.arbre_05);
break;
case 6:
component.SetBackgroundResource(Resource.Drawable.arbre_06);
break;
case 7:
component.SetBackgroundResource(Resource.Drawable.arbre_07);
break;
}
}
通过此链接,是否可以在Xamarin中使用类似Java方法的内容?
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
不是您的应用释放内存的原因,而是一种减少OutOfMemoryError可能性的方法。
我看到android要为未压缩的图像分配“ 44236812”字节(44兆字节),该图像可能来自2048 * 1536像素1.6MB jpg文件。
您是否尝试过减少资源图像(每个像素更少的像素或更少的位)?您的设备能够显示这种高分辨率吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句