不能使用Parseint

时间:2014-11-13 15:10:52

标签: android parseint

我只是一个初学者。每当我运行这个程序时,它就会结束...... 请帮忙......

错误在下面,每当​​我删除它,程序就可以了

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    calc = (Button) findViewById(R.id.calculate);
    principal = (EditText) findViewById(R.id.princi);
    Tame = (EditText) findViewById(R.id.time);
    ratee = (EditText) findViewById(R.id.rate);

    interst = (TextView) findViewById(R.id.interest);
    amunt = (TextView) findViewById(R.id.amount);

    //The error is here 
     prncpl = principal.getText().toString();
     rte = ratee.getText().toString();
     tme = Tame.getText().toString();

     tme2 = Integer.parseInt(tme);
     prncpl2 =Integer.parseInt(prncpl);
     rte2 = Integer.parseInt(rte);
    //Ends here

2 个答案:

答案 0 :(得分:0)

你是否试图从双重解析int等?如果它是十进制值或不是整数的东西,则需要使用此方法的Double版本

Double.parseDouble(String)

答案 1 :(得分:0)

您因为尝试在空字符串上使用parseInt()而收到错误。

这些代码行:

 prncpl = principal.getText().toString();
 rte = ratee.getText().toString();
 tme = Tame.getText().toString();

 tme2 = Integer.parseInt(tme);
 prncpl2 =Integer.parseInt(prncpl);
 rte2 = Integer.parseInt(rte);

不应该在onCreate()方法中,principal.getText().toString()获取用户在EditText中键入的值,您应该调用它并在Button的onClickListener中解析它。