当我尝试用整数值填充 TextView 时,为什么我的应用程序会崩溃?

Why does my app crash when i try to populate a TextView with an integer value?

我正在尝试开发一个应用程序,将用户输入的数字与计算机随机生成的数字进行比较,根据输入数字是否高于、低于或等于生成的数字,有不同消息输出。但是,每当我尝试 运行 时,应用程序都会崩溃(使用 this stacktrace)。这是我的方法代码:

如果有人能知道它崩溃的原因,那就太好了 - Android 的新手,所以很难看到错误。

public void guessingGame (View v)
{
    EditText guess = (EditText) findViewById(R.id.ETguess);
    TextView guessError = (TextView) findViewById(R.id.guessError);
    TextView compGuess = (TextView) findViewById(R.id.tvCompGuess);

    int guessValue = Integer.parseInt(guess.getText().toString());

    if (guessValue > 20)
    {
        guessError.setVisibility(View.VISIBLE);
        guess.getText().clear();
    }
    else if (guessValue < 1)
    {
        guessError.setVisibility(View.VISIBLE);
        guess.getText().clear();
    }
    else 
    {
        int min = 1;
        int max = 20;

        Random r = new Random();
        int i = r.nextInt(max - min + 1) + min;

        int computerNumber = i;
        compGuess.setText(i);

        if (computerNumber > guessValue)
        {
            guessError.setText("Too low!");
        }
        else if (computerNumber < guessValue)
        {
            guessError.setText("Too high!");
        }
        else if (computerNumber == guessValue)
        {
            guessError.setText("Good Guess!");
        }
    }

}
    compGuess.setText(i);

您不能使用 TextView.setText(int) 将文本设置为任意整数。整数必须是字符串的资源 ID(通常在 res/values/strings.xml 中定义,或从您的上游依赖项之一导入)。

如果您想将 TextView 的内容设置为表示整数的字符串,您应该这样做

    compGuess.setText(Integer.toString(i));