Java 无法对基元类型int调用getText()
这是我为TextView设置值的代码Java 无法对基元类型int调用getText(),java,android,Java,Android,这是我为TextView设置值的代码 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.
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()出现了一个错误
无法对基元类型int调用getText()
请问我做错了什么。这个
R.id.enterHours
返回一个int
。这就是为什么当您初始化视图时
会像
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();
这个
返回一个int
。这就是为什么当您初始化视图时
会像
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();
除了codeMagic所说的之外: 在这种情况下,您应该使用字符串的变量名(reference)设置文本,而不是值(“hourValue”,除非您希望文本是hourValue) 应该是
mTextView.setText(hourValue);
除了codeMagic所说的之外: 在这种情况下,您应该使用字符串的变量名(reference)设置文本,而不是值(“hourValue”,除非您希望文本是hourValue) 应该是
mTextView.setText(hourValue);
您正在对基元类型int调用getText()。可能在
mTextView
上调用它相关:请参阅此帖子[Android:无法对基元类型int调用toString()][1][1]:您正在对基元类型int调用getText()。可能在mTextView
上调用它相关:请参阅此帖子[Android:无法调用toString())关于原始类型int][1][1]:非常感谢大家,我记下了其他问题,但无法理解答案,我是android编程新手非常感谢大家,我记下了其他问题,但无法理解答案,我是android编程新手