如何在android中修复NumberFormatException

时间:2015-04-17 05:49:36

标签: android

private void increaseFontSize() {
        String getSelction = getset.getFontSize();
        int get_selection = Integer.parseInt(getSelction);
        final CharSequence[] textSize = { "Tiny", "Small", "Medium", "Large",
                "Huge" };
        AlertDialog.Builder alert = new AlertDialog.Builder(DetailPage.this);
        alert.setTitle("Text Size");
        alert.setSingleChoiceItems(textSize, get_selection,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (textSize[which] == "Tiny") {
                            getset.setFontSize("0");
                            restartData();
                            dialog.dismiss();
                        } else if (textSize[which] == "Small") {
                            getset.setFontSize("1");
                            restartData();
                            dialog.dismiss();
                        } else if (textSize[which] == "Medium") {
                            getset.setFontSize("2");
                            restartData();
                            dialog.dismiss();
                        } else if (textSize[which] == "Large") {
                            getset.setFontSize("3");
                            restartData();
                            dialog.dismiss();
                        } else if (textSize[which] == "Huge") {
                            getset.setFontSize("4");
                            restartData();
                            dialog.dismiss();
                        }
                    }

                    private void restartData() {
                        Intent intent = new Intent(getApplicationContext(),
                                DetailPage.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                        finish();
                    }
                });
        alert.show();

    }

这是设置文本视图的字体大小的功能我已经为选择文本大小创建了对话框。但我在行String getSelction = getset.getFontSize();获取NumberFormatException;         int get_selection = Integer.parseInt(getSelction);请告诉我如何在GLobaldata课程中修复它

String fontSize;

    public String getFontSize() {
        return fontSize;
    }

    public void setFontSize(String fontSize) {
        this.fontSize = fontSize;
    }

我已将fontsize作为字符串并制作set并获取如何修复此NumberFormatException。

1 个答案:

答案 0 :(得分:2)

你必须做两件事。

第一件事:

使用.equals()进行String比较。像这样

if (textSize[which].equals("Tiny")) {

第二件事:

parseInt()之前检查您是否String is empty or not。像这样

 if(!TextUtils.isEmpty(getSelction)){

 int get_selection = Integer.parseInt(getSelction);

  }