2015-05-18 95 views
0

我有一個問題,試圖從TextView中獲取背景色並將其設置爲按鈕的背景。不能轉換爲android.graphics.drawable.ColorDrawable

我該如何解決exceletion?爲什麼我的情況不正確?

我的代碼是:

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; 
    } 
} 

但還是同樣的結果

回答

0

爲了得到您應該調用TextView的背景

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

然後將該值設置爲您的按鈕。

注意,設置背景這種方式需要API 16

+0

,我如何設置呢? 謝謝你的答案btw! –

+0

@FernandoGuimarães檢查我編輯的答案。 – Cobold

+0

我添加了我的實際代碼,結果仍然相同,如果沒有錯誤,我正在使用API​​ 21。 @Cobold –

相關問題