0

I am writing a game for android devices and currently my application only runs successfully on a tablet emulator. When I try to run the app on a phone emulator or connect it to my device it says " unfortunately, app has stopped". The following is the error that I get when I try to run the app on my phone. Any help would be appreciated.

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)
ballblue
  • 1
  • 1

1 Answers1

0

OutOfMemoryError is the most common problem occur in android while especially dealing with Bitmaps(BitmapFactory,showing trace here). This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

You can add in your manifest these lines android:hardwareAccelerated="false" , android:largeHeap="true" it is working for some situations.

<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">

I think you should read some at Androids Developer page, specially here:Displaying Bitmaps Efficiently

Read all 5 lessons and rewrite your code again. If it still don't work we will be happy to see what you've done wrong with the tutorial material.

Here some of possible answers for these type of errors in 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

java.lang.OutOfMemoryError

Solution for OutOfMemoryError: bitmap size exceeds VM budget

Community
  • 1
  • 1
W4R10CK
  • 5,502
  • 2
  • 19
  • 30