Window manager bad token exception
Use this following and you will not get Fatal Exception: android.view.WindowManager$BadTokenException
exception
if(!Email_Result.this.isFinishing())
{
customBuilder.show();
}
If you have a context
then you can use like following:
if(!((Activity) context).isFinishing())
{
customBuilder.show();
}
It seems like the Exception occurs when you invoke the show() method on the Dialog. Try using the following code which might circumvent your problem:
try {
alertDialog.show();
} catch(Exception e){
// WindowManager$BadTokenException will be caught and the app would not display
// the 'Force Close' message
}
Such a problem arises when the activity is trying to display an AlertDialog after it has already been terminated. So, you might want to look closely at how your code works.
Also, your showMessageDialog method could be simplified as follows:
public void showMessageDialog(String nMessage) {
AlertDialog.Builder customBuilder = new AlertDialog.Builder(Email_Result.this);
customBuilder.setMessage(nMessage);
customBuilder.setPositiveButton(getString(R.string.ok),new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
customBuilder.setCancelable(true);
customBuilder.show();
}
Just add if(!isFinishing) in your code like this :
private Handler h = new Handler() {
public void handleMessage(Message msg) {
if(!isFinishing)
showMessageDialog("Sorry, you cannot email entries which are earlier than one year ago.");
}
};