Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Try/Catch没有';t捕获数字格式异常_Java_Android - Fatal编程技术网

Java Try/Catch没有';t捕获数字格式异常

Java Try/Catch没有';t捕获数字格式异常,java,android,Java,Android,我是Android的新手,我已经调试了好几个小时了。我试图检查空输入,我搜索了stackoverflow的解决方案,但找不到正确的答案,这是我的代码。我的应用程序总是在没有显示catch block上的内容的情况下崩溃 public class LengthActivity extends ActionBarActivity { Length len = new DoLengthConversion(); Spinner spinner; Button getAnswe

我是Android的新手,我已经调试了好几个小时了。我试图检查空输入,我搜索了stackoverflow的解决方案,但找不到正确的答案,这是我的代码。我的应用程序总是在没有显示catch block上的内容的情况下崩溃

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();
        }
    }
}
/**/
公共类长度活动扩展了ActionBarActivity{
长度len=新的长度转换();
纺纱机;
按钮获取答案;
编辑文本用户输入;
文本视图um;
文本视图mm;
文本视图cm;
文本视图dm;
文本视图m;
文本视图知识管理;
文本视图英寸;
文本视图ft;
TextView-yd;
文本视图英里;
文本视图nm;
双输入;
字符串[]单位={“微米”、“毫米”、“厘米”、“分米”、“米”、“公里”、“英寸”、“英尺”、“码”、“英里”、“纳米”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
试一试{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_length);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
微调器=(微调器)findViewById(R.id.spinner);
getComboBox();
initControls();
}捕获(数字格式){
AlertDialog.Builder ex=新建AlertDialog.Builder(此);
例如setTitle(“遇到错误:”);
例如setMessage(例如getMessage());
例如,可设置可取消(true);
例如,设置正按钮(“确定”,空);
例如,setNegativeButton(“报告问题”,null);
例如create().show();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\U长度,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
返回意图=新意图(getApplicationContext(),BasicActivity.class);
startActivityForResult(返回,0);
覆盖转换(R.anim.abc\u淡入,R.anim.abc\u淡出);
完成();
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
公共控件(){
试一试{
userInput=(EditText)findViewById(R.id.userInput);
initiateTextViews();
getAnswer=(按钮)findViewById(R.id.getAnswer);
getAnswer.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
input=Double.parseDouble(userInput.getText().toString());
int pos=spinner.getSelectedItemPosition();
如果(位置==0){
打印转换();
}否则如果(位置==1){
PrintMillerConversions();
}否则如果(位置==2){
打印转换();
}否则如果(位置==3){
printDecimeterConversions();
}否则如果(位置==4){
printMeterConversions();
}否则,如果(位置==5){
PrintKimberConversions();
}否则如果(位置==6){
printInchConversions();
}否则如果(位置==7){
printFtConversion();
}否则如果(位置==8){
printYdConversion();
}否则如果(位置==9){
printMileConversion();
}否则,如果(位置==10){
printNmConversion();
}
}
});
}捕获(数字格式){
AlertDialog.Builder ex=新建AlertDialog.Builder(此);
例如setTitle(“遇到错误:”);
例如setMessage(例如getMessage());
例如,可设置可取消(true);
例如,设置正按钮(“确定”,空);
例如,setNegativeButton(“报告问题”,null);
例如create().show();
}
}
私有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);
英寸=(文本视图)findViewById(R.id.inch);
ft=(TextView)findViewById(R.id.ft);
yd=(TextView)findViewById(R.id.yd);
英里=(文本视图)findViewById(R.id.miles);
nm=(TextView)findViewById(R.id.nm);
}
私有void getComboBox(){
试一试{
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器项目,单位);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
}捕获(阵列索引边界外异常e){
AlertDialog.Builder ex=新建AlertDialog.Builder(此);
例如setTitle(“遇到错误:”);
例如,setMessage(例如toString());
例如,可设置可取消(true);
例如,设置正按钮(“确定”,空);
例如,setNegativeButton(“报告问题”,null);
例如create().show();
}
}
}
/**/
我要做什么
.........
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.....
       }
    }
}
.
.