我正在为Android设备编写游戏,目前我的应用程序只能在平板电脑模拟器上成功运行。当我尝试在手机模拟器上运行应用程序或将其连接到我的设备时,它说“不幸的是,应用已经停止”。以下是我尝试在手机上运行应用程序时出现的错误。任何帮助将不胜感激。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: asdf.test, PID: 30171
java.lang.OutOfMemoryError: Failed to allocate a 20071980 byte allocation with 2044256 free bytes and 1996KB 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:4215)
at android.content.res.Resources.loadDrawable(Resources.java:4089)
at android.content.res.Resources.loadDrawable(Resources.java:3939)
at android.content.res.TypedArray.getDrawable(TypedArray.java:886)
at android.widget.ImageView.<init>(ImageView.java:157)
at android.widget.ImageView.<init>(ImageView.java:145)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at asdf.test.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
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)
答案 0 :(得分:0)
OutOfMemoryError 是Android中最常见的问题,尤其是在处理Bitmaps(BitmapFactory,showing trace here)
时。当由于内存空间不足而无法分配对象时,Java虚拟机(JVM)会抛出此错误,并且垃圾收集器也无法释放一些空间。
您可以在清单中添加这些行android:hardwareAccelerated="false"
,android:largeHeap="true"
,它适用于某些情况。
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
我认为你应该在Androids Developer页面上阅读一些内容,特别是在这里:Displaying Bitmaps Efficiently
阅读所有5节课,然后重新编写代码。如果它仍然无法工作,我们将很高兴看到您在教程材料中做错了什么。
这里有一些SOF中这类错误的可能答案
Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap
How to solve java.lang.OutOfMemoryError trouble in Android
Android : java.lang.OutOfMemoryError
Solution for OutOfMemoryError: bitmap size exceeds VM budget