How to display AlertDialog in a fragment?

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?

+47
android android-context fragment android-alertdialog
Apr 18 '12 at 10:08
source share
6 answers

Replace context with getActivity() .

ApplicationContext should not be used for tasks such as creating dialog boxes. Since you are in a fragment, you can get an Activity-Context instead by simply calling the Fragments getActivity() method.

+112
Apr 18 '12 at 10:12
source share

Additional information about this question (AlertDialog in fragment managed inside the event):

If you call AlertDialog inside an event like onClick (View v) or onLongClick (View v), you can use

 public boolean onClick(View v) { ... AlertDialog.Builder alertBuilder = new AlertDialog.Builder(v.getContext()); ... } 
+9
May 24 '14 at 12:34
source share

Try using DialogFragment, DialogFragment is better if you use fragments

+7
Apr 18 '12 at 10:12
source share

I used it in the adapter inside the listView, so I could not use getActivity() . To do this, I used getActivity() for context when creating the adapter in the fragment:

 this.adapter = new myAdapter(getActivity(), factory); 

Later in another class (adapter class) I was able to use getContext() , and it worked.

0
Apr. 19 '14 at 8:56
source share

You can try this or use DialogFragment

 private void showAlert(final int position) { new AlertDialog.Builder(getActivity().getApplicationContext()) .setTitle("Delete entry") .setMessage("Are you sure you want to delete this entry?") .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // deleteSuggestions(position); } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // do nothing } }) .setIcon(android.R.drawable.ic_dialog_alert) .show(); } 
0
May 18 '17 at 11:13
source share
  AlertDialog alert= null; AlertDialog.Builder build= new AlertDialog.Builder(getActivity()); build.setTitle("title"); build.setItems(stringarrayname, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //Toast.makeText(getActivity(), "hi", Toast.LENGTH_SHORT).show(); } }); build.create().show(); 
-one
Sep 23 '14 at 7:14
source share



All Articles