Android Switch声明崩溃

时间:2014-11-28 03:23:24

标签: java android debugging switch-statement

我在我的android java文件中创建了一个switch语句,交换机会根据相应的'等级将一个可绘制文件设置为imageView。当它运行时崩溃并说它内存不足但是我已经分配了虚拟设备3000mb并且代码运行正常,直到我输入一个值来进行比较

调试程序错误消息

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)

切换声明 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;
    }

顺便说一下,分配给每个案例的所有图像都非常小,每个只有大约15kb。我还想知道是否有更简单的方法来实现它?

1 个答案:

答案 0 :(得分:2)

问题与switch本身无关,由于图片大小,您的内存不足。 Android进程具有OS(堆)分配给它们的最大内存量。该内存与设备的存储空间无关。

This回答显示某些设备的堆大小。