0

I created a switch statement in my android java file, the switch will set a drawable file to an imageView depending on the corresponding 'rating'. When it runs it crashes and says that it ran out of memory however I have allocated the virtual device 3000mb and the code runs fine until I input a value for the switch to compare

Debugger Error Message

1752-1752/com.cs13ppz.virtualgallery E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.cs13ppz.virtualgallery, PID: 1752
    java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
            at android.content.res.Resources.loadDrawable(Resources.java:2110)
            at android.content.res.Resources.getDrawable(Resources.java:700)
            at android.widget.ImageView.resolveUri(ImageView.java:638)
            at android.widget.ImageView.setImageResource(ImageView.java:367)
            at com.cs13ppz.virtualgallery.ArtworkLibrary.ratingBar(ArtworkLibrary.java:55)
            at com.cs13ppz.virtualgallery.ArtworkLibrary.onCreate(ArtworkLibrary.java:18)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Switch Statement ImageView ratingBar = (ImageView) findViewById(R.id.ratingbar); int rating = 5;

switch(rating){
    case 0: {
        ratingBar.setImageResource(R.drawable.star0);
    }
    case 1: {
        ratingBar.setImageResource(R.drawable.star05);
    }
    case 2: {
        ratingBar.setImageResource(R.drawable.star1);
    }
    case 3:
            ratingBar.setImageResource(R.drawable.star15);
            break;
    case 4:
            ratingBar.setImageResource(R.drawable.star2);
            break;
    case 5:
            ratingBar.setImageResource(R.drawable.star25);
            break;
    case 6:
            ratingBar.setImageResource(R.drawable.star3);
            break;
    case 7:
            ratingBar.setImageResource(R.drawable.star35);
            break;
    case 8:
            ratingBar.setImageResource(R.drawable.star4);
            break;
    case 9:
            ratingBar.setImageResource(R.drawable.star45);
            break;
    case 10:
            ratingBar.setImageResource(R.drawable.star5);
            break;
    }

By the way all the images that are being assigned to each case are very small, only about 15kb each. I was also wondering if there would be an easier way to implement this?

Phillip
  • 59
  • 3
  • 9
  • 2
    I don't think this is the problem, but is there a reason you don't have any breaks after conditions 0, 1 and 2? You really need them, or the code will 'fall through' https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – user384842 Nov 28 '14 at 03:37
  • I do now, i had just edited the code and forgot to add them back in – Phillip Nov 28 '14 at 20:50

1 Answers1

2

The problem is not related with the switch per se, you are running out of memory due to the size of your images. Android process have a maximum amount of memory allocated to them by the OS (the heap). This memory has nothing to do with the storage space of the device.

This answer shows heap sizes for some devices.

Community
  • 1
  • 1
Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • Really the images are only 20kb each, and even if I delete all the other cases and try with 1 case it still fails. I had a look at the link and have tried the android:setheapsize(bigger) thing but it still didn't work – Phillip Nov 28 '14 at 21:30