【问题标题】:Getting java.lang.String cannot be cast to java.lang.Integer Error获取 java.lang.String 无法转换为 java.lang.Integer 错误
【发布时间】:2020-07-15 15:15:46
【问题描述】:

我一直在这里寻找答案,但我似乎找不到我的代码有什么问题。如果我将 positionOfCorrectAnswer 和 getTag 转换为字符串,那么代码工作正常,但如果两者都是整数类型,我会在编译器中收到错误 java.lang.String cannot be cast to java.lang.Integer。

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));
    }

【问题讨论】:

    标签: java android


    【解决方案1】:

    使用Integer.parseInt 将String 转换为int。你不能简单地施放它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多