2013-10-14 54 views
0

蔭讓步的EditText一些值,但如果我嘗試使用backspack按鈕將其刪除,並進入新的value..It越來越ForceClosed.How來糾正這個問題如何制止「強制關閉」的錯誤

我的代碼如下

empty_cyl_recvd = (EditText) findViewById(R.id.empty_cyl_recvd); 
new_cyl_recvd = (EditText) findViewById(R.id.new_cyl_recvd); 
filled_cyl_unload = (EditText) findViewById(R.id.filled_cyl_unload);`enter code here` 
    dmg_cyl_recvd = (EditText) findViewById(R.id.dmg_cyl_recvd);  

    total_dmg_cyl=(EditText)findViewById(R.id.sdff); 
    total_empty_cyl=(EditText)findViewById(R.id.wertyu);  
    total_filled_cyl=(EditText)findViewById(R.id.gfhgftg); 

empty_cyl_recvd.addTextChangedListener(new TextWatcher() 
     { 

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

      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, 
        int arg2, int arg3) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, 
        int arg3) { 

    int a=Integer.parseInt(empty_cyl_recvd.getText().toString());    int b=Integer.parseInt(Util.EMPTY_LIST.get(0).toString()); 
       int c=a+b; 
       total_empty_cyl.setText(""+c); 


          }}); 

     filled_cyl_unload.addTextChangedListener(new TextWatcher() 
     { 

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

      } 

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

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 

       int a=Integer.parseInt(filled_cyl_unload.getText().toString()); 
       int b=Integer.parseInt(Util.FILL_LIST.get(0).toString()); 
       int c=a+b; 
       total_filled_cyl.setText(""+c); 


     }); 


     dmg_cyl_recvd.addTextChangedListener(new TextWatcher(){ 

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

      } 

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

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 

       int a=Integer.parseInt(dmg_cyl_recvd.getText().toString()); 
       int b=Integer.parseInt(Util.DAMAGE_LIST.get(0).toString()); 
       int c=a+b; 
           total_dmg_cyl.setText(""+c); 


     });`` 
+0

在解析爲onTextChanged中的整數之前,爲所有EditText添加空檢查。 –

+0

你想達到什麼目的?想過濾一些東西? –

+0

停止強制關閉放在try {} catch(){}塊內的TextChanged()代碼。 –

回答

0

首先我建議你使用調試器,這將幫助你快速捕捉這些問題。

在onTextChange的代碼中可能存在異常(NPE/NumberFormat/ArrayIndexOutOfBounds)。因此,要適當檢查此代碼

int a,b; 
if(dmg_cyl_recvd.getText() != null && !dmg_cyl_recvd.getText().trim().equals("")) // also put it in try-catch for the case the input is not numeric 
try{ 
    a=Integer.parseInt(dmg_cyl_recvd.getText().toString()); 
}catch(NumberFormatException ex){ 
//alert user about wrong input 
} 
if(Util.DAMAGE_LIST.size() > 0)//assuming it is a list, if not use method that gives the size of corresponding collection 
    b=Integer.parseInt(Util.DAMAGE_LIST.get(0).toString()); 
0

添加適當的異常處理以避免強制關閉。當Integer.parseInt(..)獲取空/無效文本時,您可能會遇到NumberFormatException。