So I’m writing a piece of software and it keeps crashing when I try to create a second drawable. It starts out on the front screen, then I push a button and it goes to a new activity. On that activity I have an ImageView in the middle and a button on the bottom. When the activity loads there is a drawable in the ImageView. So when I click on the button at the bottom it is supposed to change the drawable that is in the ImageView but it crashes. I found errors in the LogCat that say things like:
E/AndroidRuntime(1385): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
So here is my code from that activity:
package com.foocity.android.gui;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class NewMapActivity extends Activity
{
public ImageView mapPreview;
public Drawable currentPic;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newmap);
currentPic = getResources().getDrawable(R.drawable.milestone_cleanenergy);
mapPreview = (ImageView) findViewById(R.id.chooseCanvas);
mapPreview.setImageDrawable(currentPic);
}
public void changePic(View view)
{
System.out.println("tester boy!!!!!!!!!!!");
currentPic = null;
currentPic = getResources().getDrawable(R.drawable.welcomescreen);
mapPreview.setImageDrawable(currentPic);
}
}
And here is what the LogCat says when it crashes:
06-13 21:25:56.613: D/dalvikvm(4086): GC_EXTERNAL_ALLOC freed 614 objects / 50072 bytes in 58ms
06-13 21:26:02.133: W/System.err(4086): java.io.FileNotFoundException: /sdcard/000.txt (No such file or directory)
06-13 21:26:02.143: W/System.err(4086): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
06-13 21:26:02.143: W/System.err(4086): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
06-13 21:26:02.143: W/System.err(4086): at java.io.FileInputStream.<init>(FileInputStream.java:82)
06-13 21:26:02.143: W/System.err(4086): at java.util.Scanner.<init>(Scanner.java:188)
06-13 21:26:02.143: W/System.err(4086): at java.util.Scanner.<init>(Scanner.java:167)
06-13 21:26:02.143: W/System.err(4086): at com.foocity.android.Map.<init>(Map.java:33)
06-13 21:26:02.143: W/System.err(4086): at com.foocity.android.FooCity.setMap(FooCity.java:594)
06-13 21:26:02.143: W/System.err(4086): at com.foocity.android.gui.StartActivity.chooseMap(StartActivity.java:33)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:02.153: W/System.err(4086): at android.view.View$1.onClick(View.java:2067)
06-13 21:26:02.153: W/System.err(4086): at android.view.View.performClick(View.java:2408)
06-13 21:26:02.153: W/System.err(4086): at android.view.View$PerformClick.run(View.java:8816)
06-13 21:26:02.153: W/System.err(4086): at android.os.Handler.handleCallback(Handler.java:587)
06-13 21:26:02.153: W/System.err(4086): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 21:26:02.153: W/System.err(4086): at android.os.Looper.loop(Looper.java:123)
06-13 21:26:02.153: W/System.err(4086): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:02.153: W/System.err(4086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-13 21:26:02.153: W/System.err(4086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-13 21:26:02.153: W/System.err(4086): at dalvik.system.NativeStart.main(Native Method)
06-13 21:26:02.393: D/dalvikvm(4086): GC_EXTERNAL_ALLOC freed 3101 objects / 159528 bytes in 53ms
06-13 21:26:07.113: I/System.out(4086): tester boy!!!!!!!!!!!
06-13 21:26:07.123: E/dalvikvm-heap(4086): 9469350-byte external allocation too large for this process.
06-13 21:26:07.123: E/GraphicsJNI(4086): VM won't let us allocate 9469350 bytes
06-13 21:26:07.133: D/AndroidRuntime(4086): Shutting down VM
06-13 21:26:07.133: W/dalvikvm(4086): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-13 21:26:07.143: E/AndroidRuntime(4086): FATAL EXCEPTION: main
06-13 21:26:07.143: E/AndroidRuntime(4086): java.lang.IllegalStateException: Could not execute method of the activity
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View$1.onClick(View.java:2072)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View.performClick(View.java:2408)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View$PerformClick.run(View.java:8816)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.os.Handler.handleCallback(Handler.java:587)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.os.Looper.loop(Looper.java:123)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:07.143: E/AndroidRuntime(4086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-13 21:26:07.143: E/AndroidRuntime(4086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-13 21:26:07.143: E/AndroidRuntime(4086): at dalvik.system.NativeStart.main(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): Caused by: java.lang.reflect.InvocationTargetException
06-13 21:26:07.143: E/AndroidRuntime(4086): at com.foocity.android.gui.NewMapActivity.changePic(NewMapActivity.java:30)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View$1.onClick(View.java:2067)
06-13 21:26:07.143: E/AndroidRuntime(4086): ... 11 more
06-13 21:26:07.143: E/AndroidRuntime(4086): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.nativeCreate(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.content.res.Resources.loadDrawable(Resources.java:1709)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.content.res.Resources.getDrawable(Resources.java:581)
06-13 21:26:07.143: E/AndroidRuntime(4086): ... 15 more
It obviously has something to do with memory management, which I’m not use to monitoring with Java, but I’m not really sure what to do. Any help would be appreciated. Thanks
P.S. I realized as posting this that I tried something else (creating a file object and then writing it’s content to System.out) and it caused the same error even before the new activity loaded the first drawable in the ImageView so it must have everything to do with memory management, I guess I need help with how to manage memory in this system because I am used to the GC taking care of that in regular Java.
From this line it is evident that image you are trying to display is excessive size. Try to reduce the size of the image and see.