2011-12-29 37 views
1

我有1周的EditText與數字輸入only.the問題是,假設用戶類型1234沒有在它和afterwrds如果他想改變它,對,當他西港島線按Del鍵達時間汶河日冕物質拋射在2然後按一次DEL鍵,應用程序crashes.and我想如果處理文本的EditText長度爲0的情況,但也沒有還是老樣子wrkng 這裏是我的代碼的EditText崩潰應用

input.addTextChangedListener(new TextWatcher() {    
     public void onTextChanged(CharSequence s, int start, 
           int before, int count) 
     { 
      final String in= input.getText().toString();//input is edittext 
      final int j=in.length(); 
      Cursor ansof1=(Cursor) mSpinner.getSelectedItem();//1st spinner tks 1 value 
      String temp=ansof1.getString(1); 

      Cursor ansof2=(Cursor)mSpinner2.getSelectedItem();//for 2 spinner 
      String temp2=ansof2.getString(1); 
      Cursor cn = myDbHelper.selectcur(temp); 
      double ans1=cn.getDouble(3); 
      Cursor cm=myDbHelper.selectcur(temp2); 
      double ans2=cm.getDouble(3); 
      no = Integer.parseInt(in); 

     final double finalans=((ans1/ans2)*no); 
     NumberFormat formatter = new DecimalFormat("##,##,###"); 
    if(temp.equalsIgnoreCase(temp2)) 
     { 

      //dlgAlert.setMessage("OOpss..!! Both Currencies Are Same...!!"); 
      text1.setText(no+" "+temp+" "+"="+" "+no+" "+temp2); 

      //dlgAlert.create().show(); 

     } 
     else 

     text1.setText(no+" "+temp+" "+"="+" "+formatter.format(finalans)+" "+temp2); 


     } 

     @Override 
     public void afterTextChanged(Editable arg0) 
     { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) 
     { 
      // TODO Auto-generated method stub 

     } 
    }); 
+1

添加一個try/catch塊並記錄這個異常嗎? – rfsk2010 2011-12-29 12:54:28

回答

2

這是因爲你嘗試轉換空字符串到數字。所以檢查這樣的字符串的長度,

input.addTextChangedListener(new TextWatcher() {    
     public void onTextChanged(CharSequence s, int start, 
           int before, int count) 
     { 
      if(s.length()==0) 
      { 
       return; 
      } 
      else 
      { 
        // your code here 
      } 
} 
+0

-yup我得到that..still日Thnx很多.. – sups 2011-12-29 13:14:42

+0

一般好知道你已經找到自己的解決方案,但StackOverflow的改善,我建議你到時候你問的問題和發現自己回答(大!!)..然後交它作爲回答,並選擇您own..cause某個時候會發生什麼問題,你面臨着一些其他的也和他們可以從你的解決方案。 – MKJParekh 2011-12-29 13:20:17

+0

唯一正確答案!非常感謝! – 2013-03-01 11:12:31