如何在android java中将数字字符串值转换为整数?

use*_*654 0 java android

我有这个代码:

 public void display(View view) {
    EditText edC = (EditText) findViewById(R.id.edC);
    TextView tvD = (TextView) findViewById(R.id.tvDisplay);
    try {
        String creditsS = edC.getText().toString();
        int credits = Integer.valueOf(creditsS);
        tvD.setText(credits);   
    } catch(NumberFormatException nfe) {
        tvD.setText("Couldn't parse.");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行程序并单击按钮时,应用程序崩溃.我也尝试过

int credits = Integer.parseInt(creditsS)
Run Code Online (Sandbox Code Playgroud)

不起作用.哪里出了问题?

顺便说一下,我无法打印堆栈跟踪,因为logcat继续显示无限循环等错误.谢谢.:)

FD_*_*FD_ 5

该应用程序因此行崩溃:

tvD.setText(credits);
Run Code Online (Sandbox Code Playgroud)

问题是你setText()用一个int参数调用.该方法被定义为接受ints,因此编译器不会抱怨,但这些ints应该是字符串资源的 id .如果您setText()使用任何随机调用int,该应用程序会尝试查找具有该ID 的字符串资源,并Resources$NotFoundException在执行此操作时崩溃.

只需将该行更改为以下内容:

tvD.setText("" + credits);
Run Code Online (Sandbox Code Playgroud)