应用程序由于空白而重复崩溃EditText

问题描述:

我创建了一个简单的应用程序来计算折扣。在提供值时它工作正常,但在用空文本字段进行计算时会一直崩溃。提前致谢..!!应用程序由于空白而重复崩溃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

尝试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) 

        { 

        } 

      } 



       } 

      }); 

当您尝试计算折扣,你叫ed1.getText()然后,您尝试将空值转换为双精度值,女巫导致NullPointerException。 为了解决这个问题,你必须检查getText()方法是否返回一个有效的文本来转换它。

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

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); 
      } 
     } 
    } 
}); 

使用试戴抓在这样简单的计算,不建议和可能“隐藏” crusial错误。首先,从点击事件中的If语句开始,并在其中执行所有检查。 (if txtValue.getText()!= null {do something})。这样,您可以更轻松地调试和更正您的代码。

所以,只是为了澄清;删除try-catch。在进行任何计算之前,如果确定并检查您的txt字段是否为空,请在没有任何问题和有效计算的情况下单击事件。