Try / Catch没有捕获NumberFormatException

时间:2015-07-07 06:05:08

标签: java android

我是Android新手,我正在调试这几个小时。我正在尝试检查空输入,我搜索stackoverflow寻找解决方案,但我找不到正确答案这里是我的代码。我的应用程序总是崩溃而不显示catch块上的内容。

public class LengthActivity extends ActionBarActivity {

    Length len = new DoLengthConversion();
    Spinner spinner;
    Button getAnswer;
    EditText userInput;
    TextView um;
    TextView mm;
    TextView cm;
    TextView dm;
    TextView m;
    TextView km;
    TextView inch;
    TextView ft;
    TextView yd;
    TextView mile;
    TextView nm;
    double input;
    String[] units = { "micrometer", "millimeter", "centimeter", "decimeter", "meter", "kilometer", "inch", "feet", "yard", "mile", "nanometer" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_length);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            spinner = (Spinner) findViewById(R.id.spinner);
            getComboBox();
            initControls();

        } catch (NumberFormatException e) {

            AlertDialog.Builder ex = new AlertDialog.Builder(this);
            ex.setTitle("Error Encountered: ");
            ex.setMessage(e.getMessage());
            ex.setCancelable(true);
            ex.setPositiveButton("OK", null);
            ex.setNegativeButton("Report problem", null);
            ex.create().show();

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_length, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        Intent back = new Intent(getApplicationContext(), BasicActivity.class);
        startActivityForResult(back, 0);
        overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
        finish();

        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        // noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {

            return true;

        }

        return super.onOptionsItemSelected(item);

    }

    public void initControls() {

        try {

            userInput = (EditText) findViewById(R.id.userInput);
            initiateTextViews();
            getAnswer = (Button) findViewById(R.id.getAnswer);
            getAnswer.setOnClickListener(new Button.OnClickListener() {

                public void onClick(View v) {
                    input = Double.parseDouble(userInput.getText().toString());
                    int pos = spinner.getSelectedItemPosition();
                    if (pos == 0) {
                        printMicronConversions();
                    } else if (pos == 1) {
                        printMillimeterConversions();
                    } else if (pos == 2) {
                        printCentimeterConversions();
                    } else if (pos == 3) {
                        printDecimeterConversions();
                    } else if (pos == 4) {
                        printMeterConversions();
                    } else if (pos == 5) {
                        printKilometerConversions();
                    } else if (pos == 6) {
                        printInchConversions();
                    } else if (pos == 7) {
                        printFtConversion();
                    } else if (pos == 8) {
                        printYdConversion();
                    } else if (pos == 9) {
                        printMileConversion();
                    } else if (pos == 10) {
                        printNmConversion();
                    }

                }

            });

        } catch (NumberFormatException e) {

            AlertDialog.Builder ex = new AlertDialog.Builder(this);
            ex.setTitle("Error Encountered: ");
            ex.setMessage(e.getMessage());
            ex.setCancelable(true);
            ex.setPositiveButton("OK", null);
            ex.setNegativeButton("Report problem", null);
            ex.create().show();

        }
    }

    private void initiateTextViews() {

        um = (TextView) findViewById(R.id.um);
        mm = (TextView) findViewById(R.id.mm);
        cm = (TextView) findViewById(R.id.cm);
        dm = (TextView) findViewById(R.id.dm);
        m = (TextView) findViewById(R.id.m);
        km = (TextView) findViewById(R.id.km);
        inch = (TextView) findViewById(R.id.inch);
        ft = (TextView) findViewById(R.id.ft);
        yd = (TextView) findViewById(R.id.yd);
        mile = (TextView) findViewById(R.id.miles);
        nm = (TextView) findViewById(R.id.nm);

    }

    private void getComboBox() {

        try {

            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, units);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

        } catch (ArrayIndexOutOfBoundsException e) {

            AlertDialog.Builder ex = new AlertDialog.Builder(this);
            ex.setTitle("Error Encountered: ");
            ex.setMessage(e.toString());
            ex.setCancelable(true);
            ex.setPositiveButton("OK", null);
            ex.setNegativeButton("Report problem", null);
            ex.create().show();
        }
    }
}
/**/

我尝试的是if(Double.toString(input).equals("")){},但没有运气。编译器告诉我invalid double:""

1 个答案:

答案 0 :(得分:1)

只是一个简单的检查文本框是否具有值。

.........
public void onClick(View v) 
{
    if(!userInput.getText().toString().trim().equals(""))
    {
       //Write your code here... 
       try
       {
           input = Double.parseDouble(userInput.getText().toString());
       }
       catch(Exception ex)
       {
           //Handle exception here.....
       }
    }
}
.
.