How to display AlertDialog in a Fragment?
Solution 1
Replace context
with getActivity()
.
The ApplicationContext
should not be used for tasks such as creating Dialogs. As you are in a fragment you can instead get the Activity-Context simply by calling the Fragments getActivity()
method.
Solution 2
More Information about this question (AlertDialog in a fragment, managed inside an event):
If you call AlertDialog within 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());
...
}
Solution 3
Try to use DialogFragment, DialogFragment is better when you use Fragments
Solution 4
I have had similar issues whereby I was trying to create an AlertDialog from a Fragment. A NullPointerException arose from it. Initially I did as follows:
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
The NullPointerException
occurred specifically when calling alertDialog.show()
later on in the code.
But after searching the documentation for AlertDialog.Builder()
, there seemed to be another way to initialize it [AlertDialog.Builder Doc], which is to include a theme/resId as shown below:
AlertDialog alertDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_AppCompat_Dialog_Alert).create();
This resolved the NullPointerException
at hand. Hope this helps you as well!
andro-girl
Updated on July 08, 2022Comments
-
andro-girl almost 2 years
I want to display an alert dialog in my app. I am using fragments. I tried the below code to do this:
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 was crashing and the error in logcat was:
04-18 15:23:01.770: E/AndroidRuntime(9424): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
From internet I came to know that the crash is due to context issue. I had given context as
context = this.getActivity().getApplicationContext();
I don't know what is the problem with this. Can anybody help me?
-
andro-girl about 12 yearscan you please post a sample code for that?this is the first time i am hearing about DialogFragment
-
Sujith Thankachan over 10 yearsWhat should do if getActivity() is null?
-
udaysagar almost 10 yearsi got the alert dialog by using this code in fragments
-
Mousa over 8 years@SujithPt When
getActivity()
returns null, it means that the fragment is not attached to the activity and you should not show any dialogs. -
Rodolfo Abarca over 8 yearsthis code is wrong, you should use alert= build.create(); alert.show();
-
Sreekanth Karumanaghat about 6 years@SujithThankachan you can wait till the getActivity() is not null. I havenot tried it out though.
-
Soon Santos almost 6 yearsrequireActivity() new method was added in february 2018 which in case it return null it throws an exception. If you are sure this method will not return null because you are using between onAttach and onDetach you can use it to get ride ot the warning that getActivity() might return null.
-
Karan Harsh Wardhan over 5 yearslink is dead @FUBUs
-
FindOutIslamNow over 4 years@andro-girl guides.codepath.com/android/using-dialogfragment
-
BabyishTank over 2 yearsduplicate answer to the accepted answer