0

When I press Cancel (Vazgeç) button in a dialog, it disappear. But when I press it again, an error occurs. I think I couldn't dismiss the dialog. Here is my code:

sifrePencere.setTitle("Çıkış");
sifrePencere.setMessage("Uygulamayı kapatmak için lütfen şifreyi giriniz:");
sifrePencere.setView(sifre);

sifrePencere.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        String girilenSifre = sifre.getText().toString();

        SQLiteDatabase db = vt.getReadableDatabase();

        Cursor kayit = db.rawQuery("SELECT sifre FROM CocukTableti", null);
        kayit.moveToFirst();

        if (girilenSifre.equals(kayit.getString(0))) {

            android.os.Process.killProcess(android.os.Process.myPid());

        } else {

            dialog.dismiss();
            Toast.makeText(getApplicationContext(), "Girilen şifre hatalı!", Toast.LENGTH_SHORT).show();

        }

    }
});

sifrePencere.setNegativeButton("Vazgeç", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        dialog.dismiss();

    }
});

sifrePencere.show();
halilkaya
  • 467
  • 6
  • 21
  • You should probably pot the error stacktrace. – JoxTraex Feb 22 '13 at 15:10
  • *"But when I press it again, an error occurs"*, how can you click it again if the dialog is disappeared? Did you recreate it? Or did you reopen the same dialog (instance) again? – Veger Feb 22 '13 at 15:12
  • Firstly I click, it appears. Then, I press back button or cancel (vazgeç) button, it disappears as normal. However, when I press the button that opens the dialog again, error comes. – halilkaya Feb 22 '13 at 15:14

1 Answers1

0

Remove the dialog.dismiss(); from your Cancel Button deceleration and your problem will be solved.

Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27
  • 1
    `sifrePencere.setNegativeButton("Vazgeç", null)` - it will show the cancel button and the dialog will be dismissed when it is pressed. – Karakuri Feb 22 '13 at 15:49
  • By default the OK, or Cancel button will execute the code within their methods and dismiss the Dialog so don't explicitly dismiss the dialog inside these two methods. – Husam A. Al-ahmadi Feb 22 '13 at 15:54