简体   繁体   中英

Android Switch Statement Crashing

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM