2014-01-26 37 views
0

上的应用程序崩溃,我有这样的代码:当我在“完成”按钮,点击数字键盘

final EditText edC = (EditText) findViewById(R.id.etC); 
    final String creditsS = edC.getText().toString().trim(); 
    final TextView tvD = (TextView) findViewById(R.id.tvDisplay); 

    tvD.setText("Enter amount of credits"); 

    edC.setOnEditorActionListener(new OnEditorActionListener() { 

     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      int credits = Integer.parseInt(creditsS); 
      if (actionId == EditorInfo.IME_ACTION_DONE) { 
       tvD.setText("Your amount of credits" + credits); 
       return true; 
      } 
      return false; 
     } 
    }); 

,当我点击“完成”按钮,应用程序崩溃。我不明白问题在哪里。请帮忙?谢谢!!!

+0

logcat说什么? –

+0

请发布logcat。 – nhaarman

回答

0

更改您的代码:

final EditText edC = (EditText) findViewById(R.id.etC); 
final TextView tvD = (TextView) findViewById(R.id.tvDisplay); 

tvD.setText("Enter amount of credits"); 

edC.setOnEditorActionListener(new OnEditorActionListener() { 

    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     String creditsS = edC.getText().toString().trim(); 
     int credits = Integer.parseInt(creditsS); 
     if (actionId == EditorInfo.IME_ACTION_DONE) { 
      tvD.setText("Your amount of credits" + credits); 
      return true; 
     } 
     return false; 
    } 
}); 

它崩溃,因为你得到的EDC值之前有任何价值,因此它试图解析一个空字符串,并将其与NumberFormatException的崩溃。

+0

谢谢!!!!!!!!!!! – user2970654

+0

你仍然应该更好地对待用户输入的内容,但仍然可以获得'NumberFormatException'。并且不要忘记标记答案是正确的。 – Budius

相关问题