Xamarin Android 运行时异常
Xamarin Android runtime exception
我有一个 Xamarin.Android 应用程序,在她的主游戏中包含很多按钮 activity (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;
}
}
遵循此 link,是否可以在 Xamarin 中使用类似 Java 的方法?
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
如您所想,您需要正确释放图像。
即使只是多次改变方向,如果你没有正确释放它,只会分配越来越多的内存。
基本上就
imageView.SetImageDrawable(null)
应该可以解决问题。
这不是您的应用程序丢失内存的原因,而是一种降低 OutOfMemoryError 可能性的方法。
我看到 android 想为可能来自 2048*1536 像素 1.6MB jpg 文件的未压缩图像分配“44236812”字节(44 兆字节)。
您是否尝试过减少资源图像(更少的像素或更少的每像素位数)?您的设备能够显示如此高分辨率吗?
我有一个 Xamarin.Android 应用程序,在她的主游戏中包含很多按钮 activity (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;
}
}
遵循此 link,是否可以在 Xamarin 中使用类似 Java 的方法?
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
如您所想,您需要正确释放图像。 即使只是多次改变方向,如果你没有正确释放它,只会分配越来越多的内存。
基本上就
imageView.SetImageDrawable(null)
应该可以解决问题。
这不是您的应用程序丢失内存的原因,而是一种降低 OutOfMemoryError 可能性的方法。
我看到 android 想为可能来自 2048*1536 像素 1.6MB jpg 文件的未压缩图像分配“44236812”字节(44 兆字节)。
您是否尝试过减少资源图像(更少的像素或更少的每像素位数)?您的设备能够显示如此高分辨率吗?