相信你很好。在EditText字段中未输入任何内容时,您帮助其他成员在应用程序崩溃时查询(Android app crashes when nothing is entered and button is pressed)。我遇到了同样的问题,但是当我正确输入您的代码时,我的应用程序仍然崩溃。
如果您能查看下面的代码并告知我可能需要更改以使其正常工作,我将不胜感激。我对num1的用途感到困惑,你可以看到我无法将num1更改为test,因为它用于我的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();
}
答案 0 :(得分:1)
看起来你的案子已经混淆了。如果该字段为空,则只需Toast该字段为空并且不执行任何操作。如果该字段不为空,则执行所有其他操作。
//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();
}