Android 按下按钮时未输入任何内容时应用程序崩溃

Android app crashes when nothing is entered when button is pressed

相信你很好。您帮助其他成员查询 (Android app crashes when nothing is entered and button is pressed),当在 EditText 字段中未输入任何内容时应用程序崩溃。我遇到了同样的问题,但是当我正确输入您的代码时,我的应用程序仍然崩溃。

如果您能查看我下面的代码并告诉我我可能需要更改哪些内容才能使其正常工作,我将不胜感激。我对 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();
    }

看来您的案例混淆了。如果该字段为空,则只需 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();
    }