无法强制转换为android.graphics.drawable.ColorDrawable

时间:2015-05-18 22:00:44

标签: java android

我在尝试从TextView获取背景颜色并将其设置为按钮背景时遇到问题。

我该如何解决这个问题呢?为什么我的条件不正确?

我的代码是:

    public void randomBotaoCorrecto() {

            Random generator = new Random();
            int rand = generator.nextInt(3);
            TextView textElement = (TextView) findViewById(R.id.TVnomes);
            int cor = textElement.getCurrentTextColor();
            int cob1, cob2, cob3;
            Button button11 = (Button) findViewById(R.id.button1);
            Button button22 = (Button) findViewById(R.id.button2);
            Button button33 = (Button) findViewById(R.id.button3);
            ColorDrawable buttonColor1 = (ColorDrawable) button11.getBackground();
            ColorDrawable buttonColor2 = (ColorDrawable) button22.getBackground();
            ColorDrawable buttonColor3 = (ColorDrawable) button33.getBackground();

            switch (rand) {
                case 0:
                    button11.setBackgroundColor(cor);
                    do {
                        randomBotoes(button22);
                        cob2 = buttonColor2.getColor();
                    } while (cor == cob2);
                    do {
                        randomBotoes(button33);
                        cob3 = buttonColor3.getColor();
                    } while (cor == cob3 && cob3 == cob2);
                    break;

                case 1:
                    button22.setBackgroundColor(cor);
                    do {
                        randomBotoes(button11);
                        cob1 = buttonColor1.getColor();
                    } while (cob1 == cor);
                    do {
                        randomBotoes(button33);
                        cob3 = buttonColor3.getColor();
                    } while (cor == cob3 && cob3 == cob1);
                    break;

                case 2:
                    button33.setBackgroundColor(cor);

                    do {
                        randomBotoes(button22);
                        cob2 = buttonColor2.getColor();

                    } while (cor == cob2);
                    do {
                        randomBotoes(button11);
                        cob1 = buttonColor1.getColor();
                    } while (cor == cob1 && cob1 == cob2);

                    break;
            }
        }

我得到了这个结果:

 Caused by: java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable

所以,现在我有:

public void randomBotaoCorrecto() {

    Random generator = new Random();
    int rand = generator.nextInt(3);
    TextView textElement = (TextView) findViewById(R.id.TVnomes);
    Drawable cor = textElement.getBackground();
    int colorb1, colorb2, colorb3;
    Button button11 = (Button) findViewById(R.id.button1);
    Button button22 = (Button) findViewById(R.id.button2);
    Button button33 = (Button) findViewById(R.id.button3);
    ColorDrawable buttonColor1 = (ColorDrawable) button11.getBackground();
    ColorDrawable buttonColor2 = (ColorDrawable) button22.getBackground();
    ColorDrawable buttonColor3 = (ColorDrawable) button33.getBackground();

    switch (rand) {
        case 0:
            button11.setBackground(cor);
            colorb1 = buttonColor1.getColor();

            do {
                randomBotoes(button33);
                randomBotoes(button22);
                colorb2 = buttonColor2.getColor();
                colorb3 = buttonColor3.getColor();

            } while (colorb1 == colorb3 && colorb3 == colorb2  && colorb1  == colorb3 );
            break;

        case 1:
            button22.setBackground(cor);
            colorb2 = buttonColor2.getColor();

            do {
                randomBotoes(button11);
                randomBotoes(button33);
                colorb1 = buttonColor1.getColor();
                colorb3 = buttonColor3.getColor();

            } while (colorb2 == colorb3 && colorb3 == colorb1 && colorb1 == colorb2);
            break;

        case 2:
            button33.setBackground(cor);
            colorb3 = buttonColor3.getColor();
           do {
                randomBotoes(button11);
                randomBotoes(button22);
               colorb2 = buttonColor2.getColor();
               colorb1 = buttonColor1.getColor();

            } while (colorb3 == colorb1 && colorb1 == colorb2 && colorb3 == colorb2);

            break;
    }
}

但结果仍然相同

1 个答案:

答案 0 :(得分:0)

要获取TextView的背景,您应该调用

Drawable cor = textElement.getBackground();
YourButton.setBackground(cor);

然后将该值设置为按钮。

注意,以这种方式设置背景需要API 16。