I want to display a warning dialog in my application. I use fragments. I tried the code below:
AlertDialog ad = new AlertDialog.Builder(context) .create(); ad.setCancelable(false); ad.setTitle(title); ad.setMessage(message); ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); ad.show();
but it crashes and the error in logcat was:
04-18 15: 23: 01.770: E / AndroidRuntime (9424): android.view.WindowManager $ BadTokenException: Unable to add a window - the null token is not for the application
I learned from the Internet that the accident is related to a context problem. I gave context as
context = this.getActivity().getApplicationContext();
I do not know what the problem is with this. Can anybody help me?
android android-context fragment android-alertdialog
andro-girl Apr 18 '12 at 10:08 2012-04-18 10:08
source share