2013-09-10 28 views
0

調用的getText()這是我的代碼的值設置爲一個TextView無法在基本類型int

enter code here 

public void addHours(View view){ 
    final TextView mTextView = (TextView) findViewById(R.id.newHours_Value); 
    String hourValue = R.id.enterHours.getText().toint(); 
    mTextView.setText("hourValue"); 
} 

但R.id.enterHours.GetText()想出了一個錯誤

無法在原始類型上調用getText()int

請問我做錯了什麼。

+1

您是在基本類型int調用的getText()。可能是在'mTextView'上調用它 –

+0

相關:http://stackoverflow.com/questions/4953077/android-what-is-r-why-is-it-so-cryptic –

+0

看到這篇文章[Android:Can not invoke toString( )在原始類型int] [1] [1]:http://stackoverflow.com/questions/3028974/android-cannot-invoke-tostring-on-the-primitive-type-in​​t – Grmn

回答

4

R.id.enterHours 

返回int。這就是爲什麼當你初始化View你不喜歡它

TextView mTextView = (TextView) findViewById(R.id.newHours_Value); 

(TextView)其強制轉換爲TextView這樣你就可以調用它的它的方法就像getText().toString()。所以,你可能想要的是

TextView hourValueTV = (TextView) findViewById*R.id.enterHours); //assuming its a TextView and not EditText 
String hourValue = hourValueTV.getText().toString(); 
0

添加到什麼codeMagic說:

在這種情況下,你應該設置文本與字符串,而不是值(「hourValue」的變量名(參考),除非你希望你的文本是hourValue)。

應該

mTextView.setText(hourValue); 
+0

非常感謝所有,我確實注意到其他問題,但無法理解答案,我是android編程的新手 – Graham