2013-11-09 39 views
0

嗨我想問爲什麼我有一個錯誤,當我嘗試在Textview中顯示一個整數這裏是我的代碼在btnClear上工作正常,但確認它只顯示一些錯誤當我有「tvCoin.setText (「硬幣」);」碼。感謝提前的幫助在TextView中顯示整數時出錯?

  case R.id.btnConfirm: 
       if (Answer.equals(correctAnswer)) 
       { 
        tvCategory.setText("Correct"); 
        Coin = Coin + 3; 
        tvCoin.setText("Coin"); 
       } 
       else 
       { 
        tvCategory.setText("Wrong"); 
       } 
      break; 
      case R.id.btnClear: 
       Answer = ""; 
       tvAns.setText(Answer); 
      break; 
+3

什麼是錯誤? – Raghunandan

+0

什麼是錯誤?是'tvCoin'正確初始化? – codeMagic

+1

是'Coin'的一個int值,你有'tvCoin.setText(Coin)'而不是'tvCoin.setText(「Coin」);'? – Raghunandan

回答

0

如果你有一個整數,你在屏幕上打印前將其轉換爲字符串:

someinteger.toString(); 
+2

你是什麼意思在屏幕上打印 – Raghunandan

+1

我認爲混亂與標題。它說「Integer」,但這些在'setText()'方法中顯然是'String's – codeMagic

+0

是的,這是令人困惑的,但如果標題是正確的,這就是他應該做的。否則meh .. – Esqarrouth

0

替換此

int Coin; 
    tvCoin.setText(Coin); 

B'coz如果你會嘗試將Text設置爲Integer,它會拋出Exception Resources.NotFoundException

所以你應該SE像

tvCoin.setText(String.valueOf(Coin)); 
+1

我一直缺少什麼?我沒有看到'tvCoin.setText(Coin);'。我確實同意,我認爲這就是我在 – codeMagic

+1

@codeMagic之後我所認同的,只有我猜測的是與標題有關的「Coin = Coin + 3」。但會是猜測。 – Raghunandan

+0

@codeMagic是的,我同意..這可能是我們假設的唯一原因。 –

0

沒有錯代碼

tvCategory.setText("Correct"); 
Coin = Coin + 3; 
tvCoin.setText("Coin"); 

但可能是你想嘗試打印 「硬幣」 First See rule of Textview

所以,你的代碼看起來是這樣的: -

tvCoin.setText(""+Coin); 
0

在這裏在這裏你做錯tvCoin.setText("Coin");如果你做到這一點,將打印硬幣 ,如果你會做這tvCoin.setText(Coin);也錯了,它會給你喜歡ResourceNotFound異常,因爲​​不是resource id其INT variable.you不能將整數轉換爲setText()方法。正確的方法是tvCoin.setText(String.valueOf(Coin));您只能通過String values of integer