[英]Android app crashes when nothing is entered when button is pressed
相信你一切都好。 您帮助同一个成员进行了查询(当在EditText字段中未输入任何内容时,App崩溃)( Android应用程序崩溃 。 我遇到了同样的问题,但是当我正确输入代码时,我的应用程序仍然崩溃。
如果您可以在下面查看我的代码并告知我为使其正常运行而可能需要更改的内容,我们将不胜感激。 我对num1的用途感到困惑,如您所见,我无法将num1更改为测试,因为它已用于我的onClick生成电子邮件。
先感谢您。
public void calculateTS(View v){
String status;
test = Double.parseDouble(edtResult.getText().toString());
String result = String.format("%.2f", test);
Log.d("MyActivity", result);
EditText editText = (EditText)findViewById(R.id. edtResult);
Double num1 = 0.0;
final String myStr = editText.getText().toString();
if (!myStr.isEmpty())
{
num1 = Double.parseDouble(myStr);
}
else
{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.noinput),
Toast.LENGTH_LONG).show();
if( test < 20.5) {
status = "Poor";
} else if (test >= 20.5 && test < 50.5){
status = "Average";
} else if (test >= 50.5 && test < 100.0) {
status ="Well Done"; }
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Result Feedback...");
alertDialog.setMessage(status);
alertDialog.setButton("Acknowledged", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(test< 20.5)){
String email = "test@test.com";
String subject = "Feedback";
String message = "Hello,\n\nTest.";
final Intent emailIntent = new Intent( android.content.Intent.ACTION_SEND );
emailIntent.setType( "plain/text" );
emailIntent.putExtra( android.content.Intent.EXTRA_EMAIL, new String[] { email } );
emailIntent.putExtra( android.content.Intent.EXTRA_SUBJECT, subject );
emailIntent.putExtra( android.content.Intent.EXTRA_TEXT, message );
startActivityForResult( Intent.createChooser( emailIntent, "Send Mail..."), 1234);
}
}
});
alertDialog.show();
}
看来您的案件混在一起了。 如果该字段为空,则只需吐司该字段为空,什么也不做。 如果该字段不为空,则执行所有其他操作。
//This should be a member variable
Double test;
public void calculateTS(View v){
String status;
EditText editText = (EditText)findViewById(R.id.edtResult);
Double num1 = 0.0;
final String myStr = editText.getText().toString();
if (myStr.isEmpty())
{
//num1 = Double.parseDouble(myStr); //looks like this is not used?
Toast.makeText(getApplicationContext(), getResources().getString(R.string.noinput),
Toast.LENGTH_LONG).show();
}
else
{
test = Double.parseDouble(myStr);
String result = String.format("%.2f", test);
Log.d("MyActivity", result);
if( test < 20.5) {
status = "Poor";
} else if (test >= 20.5 && test < 50.5){
status = "Average";
} else if (test >= 50.5 && test < 100.0) {
status ="Well Done"; }
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Result Feedback...");
alertDialog.setMessage(status);
alertDialog.setButton("Acknowledged", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(test< 20.5)){
String email = "test@test.com";
String subject = "Feedback";
String message = "Hello,\n\nTest.";
final Intent emailIntent = new Intent( android.content.Intent.ACTION_SEND );
emailIntent.setType( "plain/text" );
emailIntent.putExtra( android.content.Intent.EXTRA_EMAIL, new String[] { email } );
emailIntent.putExtra( android.content.Intent.EXTRA_SUBJECT, subject );
emailIntent.putExtra( android.content.Intent.EXTRA_TEXT, message );
startActivityForResult( Intent.createChooser( emailIntent, "Send Mail..."), 1234);
}
}
});
alertDialog.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.