相信你一切都好。您帮助一位成员(member)解决了在 EditText 字段中未输入任何内容时应用程序崩溃的查询 ( Android app crashes when nothing is entered and button is pressed )。我遇到了同样的问题,但当我正确输入您的代码时,我的应用程序仍然崩溃。
如果您能查看下面的代码并告诉我可能需要更改哪些内容才能使其正常工作,我将不胜感激。我对 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();
}
关于java - 按下按钮时未输入任何内容时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29067655/