简体   繁体   English

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

[英]Android app crashes when nothing is entered when button is pressed

Trust you are well. 相信你一切都好。 You helped a fellow member out for a query ( Android app crashes when nothing is entered and button is pressed ) on App crashing when nothing is entered in the EditText field. 您帮助同一个成员进行了查询(当在EditText字段中未输入任何内容时,App崩溃)( Android应用程序崩溃 I am having the same issue but my application still crashes when I input your code correctly. 我遇到了同样的问题,但是当我正确输入代码时,我的应用程序仍然崩溃。

I would appreciate if you could look at my code below and inform me what I may have to change to make it work. 如果您可以在下面查看我的代码并告知我为使其正常运行而可能需要更改的内容,我们将不胜感激。 I am a confused with what the num1 was being used for, and as you can see I can't change num1 into test as it is being used for my onClick to generate an email. 我对num1的用途感到困惑,如您所见,我无法将num1更改为测试,因为它已用于我的onClick生成电子邮件。

Thank you in advance. 先感谢您。

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

It looks like you have your cases mixed up. 看来您的案件混在一起了。 If the field is empty, simply Toast that the field is empty and do nothing. 如果该字段为空,则只需吐司该字段为空,什么也不做。 If the field is not empty, then do all the other stuff. 如果该字段不为空,则执行所有其他操作。

//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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM