在编辑文本中设置文本时出现字符串资源 ID 错误

String resource ID error when setting text in edit text

我有以下代码。

if (j == 0) { // Age units.
    if (button.equals(findViewById(R.id.years)))
        q19Text.setText(yearsDiff);

    if (button.equals(findViewById(R.id.months)))
        q19Text.setText(monthsDiff);

    if (button.equals(findViewById(R.id.weeks)))
        q19Text.setText(daysDiff);

    if (button.equals(findViewById(R.id.days)))
        q19Text.setText(weeksDiff);
}

按钮是一个单选按钮。我点击的任何一个都会给我这个错误

android.content.res.Resources$NotFoundException: String resource ID #0x2344

但是,如您所见,我并没有尝试访问字符串资源文件。我正在尝试设置已实例化的编辑文本的文本。

为什么会出现此错误?

您正在使用 setText(int id) 方法。将您的整数转换为 String,您应该没问题。您还可以将 .setText("" + weeksDiff) 添加到您的代码中,它会将其视为字符串