Home > database >  Android Studio Switch label is unreachable for no apparent reason
Android Studio Switch label is unreachable for no apparent reason

Time:11-15

I'm currently trying to use a switch case statement to convert some color values in Android studios but I keep on getting marked as 'Switch Label is Unreachable'. I've tried recreating a new switch case statement and even restarting Android studio, but the problem persists.

I'm genuinely at a loss for what to do. I don't see anything that would make these statements unreachable.

Here's the aforementioned code down below:

public int getColorHex(int color)
    {
        int r = (Color.red(color) << 16) & 0x00FF0000;
        int g = (Color.green(color) << 8) & 0x0000FF00;
        int b = Color.blue(color) & 0x000000FF;

        int colorValue = 0xFF000000 | r | g | b;
        int hexValue;

        switch (colorValue) {
            case 0x000000:
                hexValue = 0;
                break;

            case 0xFFFFFF:
                hexValue = 1;
                break;

            case 0x00FF00:
                hexValue = 2;
                break;

            case 0x0000FF:
                hexValue = 3;
                break;

            case 0xFF0000:
                hexValue = 4;
                break;

            case 0xFFFF00:
                hexValue = 5;
                break;

            case 0xFF8000:
                hexValue = 6;
                break;

            case 0xFFDCC8:
                hexValue = 7;
                break;

            default:
                hexValue = 8;
                break;
        }

        return hexValue;
    }

Here's also a screenshot of the warning message Android Studio gives me.

CodePudding user response:

According to your code the high byte of colorValue is always 0xFF.

int colorValue = 0xFF000000 | r | g | b;

But in your switch cases (except 'default') the high byte is 0. So these cases are impossible to hit.

  • Related