繁体   English   中英

获取 java.lang.String 无法转换为 java.lang.Integer 错误

[英]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.parseIntString转换为int 你不能简单地施放它。

if(positionOfCorrectAnswer==Integer.parseInt(view.getTag()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM