2017-02-27 74 views
0

我創建了一個簡單的應用程序來計算折扣。在提供值時它工作正常,但在用空文本字段進行計算時會一直崩潰。提前致謝..!!應用程序由於空白而重複崩潰EditText

public class Firstctivity extends AppCompatActivity { 
    Button find; 
    EditText ed1,ed2; 
    TextView tv1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_firstctivity); 
    find=(Button)findViewById(R.id.button); 
    tv1=(TextView)findViewById(R.id.text39); 
    ed1=(EditText)findViewById(R.id.sai); 
    ed2=(EditText)findViewById(R.id.editText); 


    find.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 

      final double a =Double.parseDouble(String.valueOf(ed1.getText())); 
      final double b =Double.parseDouble(String.valueOf(ed2.getText())); 
      double results; 
      double c; 
      try { 
       c = a * b; 
       results = c/100; 
       String total2 = String.valueOf(results); 
       // String total2="fuck u"; 
       tv1.setText("You have recieved Rs" + total2 + "/- concession."); 
       tv1.setBackgroundColor(Color.parseColor("#4169E1")); 
       } 
      catch (NumberFormatException ex) 

      { 

      } 


     } 

    }); 


} 

}

+0

你有什麼試圖解決這個錯誤? – oneNiceFriend

回答

1

嘗試tihs。

find.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        if(ed1.getText().toString.length() >0){ 

        try { 
final double a =Double.parseDouble(String.valueOf(ed1.getText())); 
        final double b =Double.parseDouble(String.valueOf(ed2.getText())); 
        double results; 
        double c; 
         c = a * b; 
         results = c/100; 
         String total2 = String.valueOf(results); 
         // String total2="fuck u"; 
         tv1.setText("You have recieved Rs" + total2 + "/- concession."); 
         tv1.setBackgroundColor(Color.parseColor("#4169E1")); 
         } 
        catch (NumberFormatException ex) 

        { 

        } 

      } 



       } 

      }); 
1

當您嘗試計算折扣,你叫ed1.getText()然後,您嘗試將空值轉換爲雙精度值,女巫導致NullPointerException。 爲了解決這個問題,你必須檢查getText()方法是否返回一個有效的文本來轉換它。

0

你要檢查空字符串和空值:

find.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     if (ed1.getText() != null && !TextUtils.isEmpty(ed1.getText().toString()) && 
       ed2.getText() != null && !TextUtils.isEmpty(ed2.getText().toString())) { 

      try { 
       final double a = Double.parseDouble(String.valueOf(ed1.getText().toString())); 
       final double b = Double.parseDouble(String.valueOf(ed2.getText().toString())); 
       double c = a * b; 
       double results = c/100; 
       String total2 = String.valueOf(results); 
       // String total2="fuck u"; 
       tv1.setText("You have recieved Rs" + total2 + "/- concession."); 
       tv1.setBackgroundColor(Color.parseColor("#4169E1")); 
      } catch (NumberFormatException ex) { 
       Log.e("SOME TAG", ex.getMessage(), ex); 
      } 
     } 
    } 
}); 
0

使用試戴抓在這樣簡單的計算,不建議和可能「隱藏」 crusial錯誤。首先,從點擊事件中的If語句開始,並在其中執行所有檢查。 (if txtValue.getText()!= null {do something})。這樣,您可以更輕鬆地調試和更正您的代碼。

所以,只是爲了澄清;刪除try-catch。在進行任何計算之前,如果確定並檢查您的txt字段是否爲空,請在沒有任何問題和有效計算的情況下單擊事件。

相關問題