user1531170
user1531170

Reputation: 63

how to fix NumberFormatException in 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();

    }

this is function fro set fontsize of text view i have create dialog for select text size . but i am getting NumberFormatException at line String getSelction = getset.getFontSize(); int get_selection = Integer.parseInt(getSelction); please tell me how to fix it while i have in GLobaldata class

String fontSize;

    public String getFontSize() {
        return fontSize;
    }

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

i have take fontsize as string and make set and get please how to fix this NumberFormatException .

Upvotes: 2

Views: 68

Answers (1)

M D
M D

Reputation: 47807

You have to do two thing.

1st thing:

Used .equals() for String comparison. Like so

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

2nd thing:

Also before parseInt() check whether your String is empty or not. Like so

 if(!TextUtils.isEmpty(getSelction)){

 int get_selection = Integer.parseInt(getSelction);

  }

Upvotes: 2

Related Questions