简体   繁体   English

将Toast置于按钮上,以免崩溃

[英]Putting Toast to button so it doesn't crash

So part of my application are various calculators which I coded successfully, but if user does not input any numbers in EditText fields, application force closes. 因此,我的应用程序的一部分是成功编码的各种计算器,但是如果用户未在EditText字段中输入任何数字,则应用程序将关闭。

The second EditText field must not have value greater than 12, and Toast works there, but what should I write so the Toast is printed if user does not input values in first EditText or second, or both? 第二个EditText字段的值不能大于12,并且Toast在那里工作,但是我应该写些什么,以便如果用户没有在第一个EditText中输入值或在第二个或两个输入中都不输入值,则打印Toast?

Here's my code so you can see what I've tried: 这是我的代码,因此您可以看到我尝试过的内容:

public class Kalkulatori1RM extends AppCompatActivity {
EditText polje1, polje2;
TextView rezultat1RM;
Button btnIzracunaj1RM;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kalkulatori1_rm);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();}

    public boolean isEmpty(EditText etText) {





    polje1 = (EditText) findViewById(R.id.edit1rmTezina);
    polje2 = (EditText) findViewById(R.id.edit1rmPonavljanja);

    rezultat1RM = (TextView) findViewById(R.id.text1RMJe);

    btnIzracunaj1RM = (Button) findViewById(R.id.btnIzracunaj1RM);



    btnIzracunaj1RM.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isEmpty(polje1)||isEmpty(polje2)){
                Toast.makeText(Kalkulatori1RM.this, "Both inputs must be provided prior to calculating",Toast.LENGTH_LONG).show();
            } else {

            double prvoPolje = Double.parseDouble(polje1.getText().toString());
            double drugoPolje = Double.parseDouble(polje2.getText().toString());

            double rezultat = 0;


            if (drugoPolje == 1) {
                rezultat = prvoPolje * 1;

            } else if (drugoPolje == 2) {
                rezultat = prvoPolje * 1.05;

            } else if (drugoPolje == 3) {
                rezultat = prvoPolje * 1.08;

            } else if (drugoPolje == 4) {
                rezultat = prvoPolje * 1.11;

            } else if (drugoPolje == 5) {
                rezultat = prvoPolje * 1.15;

            } else if (drugoPolje == 6) {
                rezultat = prvoPolje * 1.18;

            } else if (drugoPolje == 7) {
                rezultat = prvoPolje * 1.20;

            } else if (drugoPolje == 8) {
                rezultat = prvoPolje * 1.25;

            } else if (drugoPolje == 9) {
                rezultat = prvoPolje * 1.30;

            } else if (drugoPolje == 10) {
                rezultat = prvoPolje * 1.33;

            } else if (drugoPolje == 11) {
                rezultat = prvoPolje * 1.37;

            } else if (drugoPolje == 12) {
                rezultat = prvoPolje * 1.43;

            } else if (drugoPolje > 12) {
                Toast.makeText(getApplicationContext(), "Broj ponavljanja ne smije biti veći od 12", Toast.LENGTH_LONG).show();

            } else {
                Toast.makeText(Kalkulatori1RM.this, "Unesite odgovarajuće podatke prije nego što pritisnete na 'Izračunaj'", Toast.LENGTH_LONG).show();
            }

            rezultat = Math.round(rezultat * 100.0) / 100.0;
            rezultat1RM.setText(Double.toString(rezultat));


        }
    }

    });

    return etText.getText().toString().trim().length() == 0;

}

Any ideas? 有任何想法吗?

This is the error: 这是错误:

02-17 20:35:08.454 1609-1609/hr.app.liftme.liftmehr E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: hr.app.liftme.liftmehr, PID: 1609
                                                                      java.lang.NumberFormatException: Invalid double: ""
                                                                          at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                          at java.lang.StringToReal.parseDouble(StringToReal.java:267)
                                                                          at java.lang.Double.parseDouble(Double.java:301)
                                                                          at hr.app.liftme.liftmehr.Kalkulatori1RM$1.onClick(Kalkulatori1RM.java:51)
                                                                          at android.view.View.performClick(View.java:4856)
                                                                          at android.view.View$PerformClick.run(View.java:19956)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:211)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

The application force closes because of the Double.parseDouble that throws NumberFormatException if the string does not contain a parsable double. 如果Double.parseDouble如果字符串不包含可分析的double则抛出NumberFormatException则应用程序强制关闭。 You have to catch the exception yourself, like shown here . 你必须自己捕获异常,如显示在这里

If you checked Logcat you would likely see that you're attempting to parse null or blank string to a double via Double.parseDouble(...) . 如果选中Logcat ,则可能会看到您正在尝试通过Double.parseDouble(...)null或空白字符串解析为double

It's best to check your input prior to performing your calculations, as such.... 最好在执行计算之前检查您的输入。

//Place in a global reference (static Utils class or something)
public boolean isEmpty(EditText etText) {
  return etText.getText().toString().trim().length() == 0;
}


btnIzracunaj1RM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
     if(isEmpty(polje1)||isEmpty(polje2)){
        Toast.makeText(this,"Both inputs must be provided prior to calculating",Toast.LENGTH_LONG).show();
     } else {
       double prvoPolje = Double.parseDouble(polje1.getText().toString());
        double drugoPolje = Double.parseDouble(polje2.getText().toString());

        double rezultat = 0;


        if (drugoPolje == 1) {
            rezultat = prvoPolje * 1;

        } else if (drugoPolje == 2) {
            rezultat = prvoPolje * 1.05;

        } else if (drugoPolje == 3) {
            rezultat = prvoPolje * 1.08;

        } else if (drugoPolje == 4) {
            rezultat = prvoPolje * 1.11;

        } else if (drugoPolje == 5) {
            rezultat = prvoPolje * 1.15;

        } else if (drugoPolje == 6) {
            rezultat = prvoPolje * 1.18;

        } else if (drugoPolje == 7) {
            rezultat = prvoPolje * 1.20;

        } else if (drugoPolje == 8) {
            rezultat = prvoPolje * 1.25;

        } else if (drugoPolje == 9) {
            rezultat = prvoPolje * 1.30;

        } else if (drugoPolje == 10) {
            rezultat = prvoPolje * 1.33;

        } else if (drugoPolje == 11) {
            rezultat = prvoPolje * 1.37;

        } else if (drugoPolje == 12) {
            rezultat = prvoPolje * 1.43;

        } else if (drugoPolje > 12) {
            Toast.makeText(getApplicationContext(), "Broj ponavljanja ne smije biti veći od 12", Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(Kalkulatori1RM.this, "Unesite odgovarajuće podatke prije nego što pritisnete na 'Izračunaj'", Toast.LENGTH_LONG).show();
        }

        rezultat = Math.round(rezultat * 100.0) / 100.0;
        rezultat1RM.setText(Double.toString(rezultat));

     }
   }
})

Another way would be to replace isEmpty(...) calls with something like tryParseDouble ... 另一种方法是用tryParseDouble这样的东西替换isEmpty(...)调用...

public boolean tryParseDouble(String str){
    try{

      double parsed = Double.parseDouble(str);
      return true;
    } catch (Exception e){
      return false;
    }
}

Which might be a better method. 这可能是一个更好的方法。

Since you have a finite number of specific choices for one of the inputs, I suggest using a Spinner instead of a TextView . 由于您对输入之一有有限的特定选择,因此建议您使用Spinner而不是TextView This way you can display the exact values used in the calculation and avoid confusion by your users. 这样,您可以显示计算中使用的确切值,并避免用户混淆。 You also no longer need to worry about erroneous input. 您也不再需要担心输入错误。

For any EditText which allows any number to be entered, you need to ensure that the input is valid. 对于允许输入任何数字的任何EditText ,您需要确保输入有效。 You can do this in one of two ways: 您可以通过以下两种方式之一执行此操作:

  1. Catch the NumberFormatException and display an error message. 捕获NumberFormatException并显示错误消息。

  2. Set the inputType for the EditText to number . EditTextinputType设置为number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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