开发者

Android Number Format Exception using EditText

开发者 https://www.devze.com 2023-02-13 11:42 出处:网络
I have a simple process running Put simply the user inputs a number into an EditText, picks Radio Button and then presses calculate. The radio buttons are connected to if statements that in turn dire

I have a simple process running

Put simply the user inputs a number into an EditText, picks Radio Button and then presses calculate. The radio buttons are connected to if statements that in turn direct towards floats that are returned replacing the number shown in the EditText. However it appears that if the calculate button is pressed before any numbers are entered then a number format exception occours causing the app to crash.

For now I will enter a default value into the EditText to decrease the chances of this happening, but i was wondering if there was a way to avoid an exception all together using something like an if statement or something similar

here is some example code

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;
import com.medialets.android.analytics.MMAnalyticsManager;

public class Area extends Activity {
    private EditText text9;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.area);
            text9 = (EditText)findViewById(R.id.EditText09);

    public void myClickHandler09(View view){
switch (view.getId()) {
case R.id.Button09:
    RadioButton SeveralRadioButtons = (RadioButton) findViewById (R.id.RadioButton901);
    float inputValue = Float.parseFloat(text9.getText().toString());
    String checkValue = String.valueOf(inputValue);

    if (checkValue.equals("")){
        text9.setText(String.valueOf(""));
   } else {

     if (SeveralRadioButtons.isChecked())  {
            text9.setText(String
                    .valueOf(conversionfactor(inputValue)));
   }
       break;
}}}

private double conversionfactor(float f){
        return f * 6.4516;
    }

Where (SeveralRadioButtons) is called there are in fact several r开发者_运维知识库adio buttons and an if statement and private double for each one respectively

As you can see I have already made an attempt at fixing the problem but the number format exception still appears.


As well as following the other suggestions for checking for null and "" values, you can also catch the NumberFormatException and handle the error case. You could abort the calculation and perhaps show an error message.

Handling the exception if it is thrown means that if there is some other edge case that you have missed, your users won't experience a full application crash.


put an if statement around your conversion checking for null (first!) or an empty string. A default value works until people put an empty string in the text box.


In the onClickListener of your button you can write an if statement that checks if the string is null or "" before actually sending the number to wherever you are sending it...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号