1

I'm developing an android game, and just added a main menu. This is a separate activity - so when someone clicks the 'new game' button on the menu, it opens a new intent to the game activity.

My problem is if the user hits 'back', it closes the game and returns to the main menu. When I hit 'New Game' again, it crashes.

I think it is attempting to make a second version of the game, and running out of memory. Should I be trying to hold a copy of the game activity in the menu, then reopen it if they hit 'New Game' again? Or is there an easier way to do this?

My "new game" button listener does the following: (IslandGame is my main game class - the bundle is just to tell it whether to start a new game or load a previous game, and which level to start in)

ButtonNewGame.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {                 
            Bundle bundle = new Bundle();
            bundle.putString("RESTORE", "NEW"); //in the restore key, say we want a new game
            bundle.putInt("CAMPAIGN", 1); //start in campaign #1

            Intent intent = new Intent(Intent.ACTION_RUN);
            intent.putExtras(bundle);
            intent.setClassName(thisActivity, IslandGame.class.getName());
            startActivity(intent);
        }         
    });

The logcat is:

06-09 17:42:34.199: E/dalvikvm-heap(239): 147456-byte external allocation too large for this process.
06-09 17:42:34.199: E/(239): VM won't let us allocate 147456 bytes
06-09 17:42:34.209: D/AndroidRuntime(239): Shutting down VM
06-09 17:42:34.209: W/dalvikvm(239): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-09 17:42:34.219: E/AndroidRuntime(239): Uncaught handler: thread main exiting due to uncaught exception
06-09 17:42:34.249: E/AndroidRuntime(239): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dylan.island/com.dylan.island.IslandGame}: android.view.InflateException: Binary XML file line #37: Error inflating class <unknown>
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.os.Looper.loop(Looper.java:123)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-09 17:42:34.249: E/AndroidRuntime(239):  at java.lang.reflect.Method.invokeNative(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239):  at java.lang.reflect.Method.invoke(Method.java:521)
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-09 17:42:34.249: E/AndroidRuntime(239):  at dalvik.system.NativeStart.main(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239): Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class <unknown>
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.Activity.setContentView(Activity.java:1622)
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.dylan.island.IslandGame.onCreate(IslandGame.java:76)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-09 17:42:34.249: E/AndroidRuntime(239):  ... 11 more
06-09 17:42:34.249: E/AndroidRuntime(239): Caused by: java.lang.reflect.InvocationTargetException
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.dylan.island.IslandView.<init>(IslandView.java:1983)
06-09 17:42:34.249: E/AndroidRuntime(239):  at java.lang.reflect.Constructor.constructNative(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.view.LayoutInflater.createView(LayoutInflater.java:500)
06-09 17:42:34.249: E/AndroidRuntime(239):  ... 23 more
06-09 17:42:34.249: E/AndroidRuntime(239): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.Bitmap.nativeCreate(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:346)
06-09 17:42:34.249: E/AndroidRuntime(239):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:372)
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.dylan.island.IslandView$IslandThread.initimages(IslandView.java:435)
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.dylan.island.IslandView$IslandThread.beginLevel(IslandView.java:220)
06-09 17:42:34.249: E/AndroidRuntime(239):  at com.dylan.island.IslandView$IslandThread.<init>(IslandView.java:215)
06-09 17:42:34.249: E/AndroidRuntime(239):  ... 27 more
gedditoffme
  • 55
  • 1
  • 7

5 Answers5

1

To avoid java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I think you need to recycle all images used in your game when destroying game main activity and coming to menu activity.

Please refer to these links for good practice when using a lot of images:

http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

Strange out of memory issue while loading an image to a Bitmap object

OutofMemoryError: bitmap size exceeds VM budget (Android)

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

try

Intent intent = new Intent(YourCurrentActivity.this, IslandGame.class);
intent.putExtra("Restore", "NEW");
intent.putExtra("CAMPAIGN", 1);
startActivity(intent);

Don't prefer the Bunlde for easy operations.

Edit: Then inside the other Activity's onCreate:

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null && extras.getInt("CAMPAIGN") == 1){
        newGame();
    }
    else{
        loadGame(extras.getInt("gameId"));
        //or whatever
    }
duru
  • 270
  • 4
  • 14
0

you can override the functionality of back button to re-lunch the main activity and set it on top of your game activity and do the same when going back to the game. but it is surly a bad practice since you don't know for sure the user is going to hit start game button again. instead try to release all used resources in an activity on it's onDestroy method and lunch a new instance when the user get's back to it. also try to minimize the size of the bitmaps you use. try 9patches for example.

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
0

You have to define the activity in your Android Manifest file so only a single instance can be created.

<activity
    android:launchMode="singleTask">
Zaid Daghestani
  • 8,555
  • 3
  • 34
  • 44
0

I think it is attempting to make a second version of the game, and running out of memory.

You are absolutely correct. If you properly finished the game activity, then it should be trying to create a second version because the first version should be gone.

Should I be trying to hold a copy of the game activity in the menu, then reopen it if they hit 'New Game' again?

In general, this is considered a horribly bad practice for a number of reasons. The most important of these is a memory leak due to contexts.

Or is there an easier way to do this?

You may define the game activity as a singleTask or singleInstance. An issue with this (at the moment) is that it is not a solution to fix memory bugs. SingleTasks are programmed differently than multiple instance applications and there are other considerations. Since your issue lies in the fact that you have memory being used in the first place, before going this route, I would definitely review the following:

  1. Make sure all resources are released. Of particular concern here is any static reference to a View. If you have them, clean them up.

  2. Make sure ALL bitmaps are recycled. Sometimes when resizing bitmaps, the original is left behind. Often a silly oversight. Just take a look. :)

  3. If you are accessing files, make sure they are closed. Almost never the problem, but always worth a try.

  4. Add a System.gc() call after your game activity is finished. This really can help after a major release of resources. Unfortunately, it is not guaranteed to. In your case, it might be best in the onResume() of your Main Menu.

Once you have exhausted all of those, then the singleTask route is simply choice of implementation.

Hope this helps,

FuzzicalLogic

Fuzzical Logic
  • 12,947
  • 2
  • 30
  • 58
  • Thanks - I could definitely do with cleaning up some memory - even the images themselves are pretty bloated, and I could reduce their size. However, I wouldn't expect for my 'game' activity to be finished - can I return to it from the menu? eg: if someone pressed back to go to the menu, then changed some settings, then returned to the middle of the game they were playing? – gedditoffme Jun 10 '12 at 03:47
  • Got it - ended up adding a confirmation window to solve any accidental 'back' pushes. After playing for a while trying to release resources, added a loop to recycle all bitmap objects in the destroy() method. That seemed to finally finish off the error. Thanks for your help. – gedditoffme Jun 11 '12 at 03:54
  • You're absolutely welcome. Glad you got it solved. Bitmap intensive applications often run into these types of errors the first time. – Fuzzical Logic Jun 11 '12 at 04:11
  • Just so you know, on this site, if an answer is especially useful to you, you should vote it up. This is true, even for answers to questions that you don't ask. Welcome to the joy of Stack Overflow! – Fuzzical Logic Jun 11 '12 at 04:14