
[英]Getting a ClassCastException: java.lang.Integer cannot be cast to java.lang.String error without casting
[英]Getting java.lang.String cannot be cast to java.lang.Integer Error
我一直潜伏在这里寻找答案,但我似乎找不到我的代码有什么问题。 If I convert positionOfCorrectAnswer
and getTag
to string then code is working fine but if both are in Integer type I am getting an error java.lang.String cannot be cast to java.lang.Integer
in the compiler.
public void chooseAnswer(View view)
{
int positionOfCorrectAnswer= rand.nextInt(4);
if(positionOfCorrectAnswer==(Integer)view.getTag())
{
score++;
resultTextView.setText("correct");
}
else
{
resultTextView.setText("Wrong");
}
question++;
scoreTextView.setText(Integer.toString(score)+"/"+ Integer.toString(question));
}
使用Integer.parseInt
将String
转换为int
。 你不能简单地施放它。
if(positionOfCorrectAnswer==Integer.parseInt(view.getTag()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.