1

I got this Error and can't catch Exception. I really confused why I can't catch , Before Works.

Here I've 2 EditText I want to check if it's empty do somework. I used other methods to check is Empty but doesn't work and Exit from app.

Code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ad=new AlertDialog.Builder(getApplicationContext()).create();
        ad.setTitle("خطا");

        sp=sp=getSharedPreferences("sp", Activity.MODE_PRIVATE);

        bsobh=(Button) findViewById(R.id.Bsobh);
        bzohr=(Button) findViewById(R.id.Bzohr);
        basr=(Button) findViewById(R.id.basr);
        bmaqrib=(Button) findViewById(R.id.bmaqrib);
        besha=(Button) findViewById(R.id.besha);
        brooze=(Button) findViewById(R.id.brooze);
        ok=(Button) findViewById(R.id.ok);

        etnamaz=(EditText) findViewById(R.id.ettedaderoozenamaz);
        etrooze=(EditText) findViewById(R.id.ettedadroozerooze);



    ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                    try{

                    int temp=Integer.parseInt(etnamaz.getText().toString());
                    sobh=temp;
                    zohr=temp;
                    asr=temp;
                    maqrib=temp;
                    esha=temp;

                    }
                    catch(Exception e){
                        ad.setMessage("لطفا تعداد روزهایی که نمازتان قضا شده است را وارد کتید");
                        ad.show();

                }

                    try{
                rooze=Integer.parseInt(etrooze.getText().toString());
                }
                    catch(Exception e){
                    ad.setMessage("لطفا تعداد روزهایی که روزه تان قضا شده است را وارد کتید");
                    ad.show();
                }

                inserted=true;
                update();


            }
        }); 
}
2
  • post where you initialized your edittext Commented Jul 20, 2014 at 2:19
  • works when edittext is not empty. Commented Jul 20, 2014 at 2:21

2 Answers 2

1
works when edittext is not empty

problem:

int temp=Integer.parseInt(etnamaz.getText().toString());

you cant parse a string to int when it is empty you will likely to get numberMismatchException.

solution:

check it first if the etnamaz or etrooze has some values before parsing it to int.

sample:

if(etnamaz.getText().length() != 0)
//if it has some values
else
//if it doesnt have value

EDIT:

instead of getting the whole application context, you need to get the activity context using the this

ad=new AlertDialog.Builder(this).create();
Sign up to request clarification or add additional context in comments.

Comments

0

I Think we cant do this. I use

Toast t =Toast.makeText(MainActivity.this, "Please ente quantity or enter 0 if none", 5000);
t.show();

Instead of

 ad.setMessage("لطفا تعداد روزهایی که روزه تان قضا شده است را وارد کتید");
                ad.show();

and Works Correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.