LuxuryWaffles
LuxuryWaffles

Reputation: 1708

Dialog in fragment crashes android app. android.view.WindowsManager$BadTokenException

I tried to create a dialog but it crashes when i try to run the codes. I want to run it when i select an item in my listview which is in my fragment, right now I just want to try showing up the dialog without passing in anything

Here is my java code:

        myInfoList.setAdapter(new CustomAdapter(details , getActivity().getApplicationContext()));

        myInfoList.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                   //System.out.println("Name: "+details.get(position).getSub();
                   //String s = details.get(position).getDescription();
                   final Dialog dialog = new Dialog(getActivity().getApplicationContext());
                   dialog.setContentView(R.layout.purchase);
                   dialog.setTitle("Title");

                   Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();
               }
       });

logcat:

07-06 12:05:16.012: V/21st Polling:(12319): clicked
07-06 12:05:16.957: D/AndroidRuntime(12319): Shutting down VM
07-06 12:05:16.957: W/dalvikvm(12319): threadid=1: thread exiting with uncaught exception (group=0x40e20930)
07-06 12:05:16.972: E/AndroidRuntime(12319): FATAL EXCEPTION: main
07-06 12:05:16.972: E/AndroidRuntime(12319): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.app.Dialog.show(Dialog.java:281)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at com.example.isellcrazyitem.SalesOrder$1.onItemClick(SalesOrder.java:104)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AbsListView.performItemClick(AbsListView.java:1102)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2751)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.widget.AbsListView$1.run(AbsListView.java:3426)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.os.Handler.handleCallback(Handler.java:725)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.os.Looper.loop(Looper.java:137)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at android.app.ActivityThread.main(ActivityThread.java:5233)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
07-06 12:05:16.972: E/AndroidRuntime(12319):    at dalvik.system.NativeStart.main(Native Method)
07-06 12:05:18.613: I/Process(12319): Sending signal. PID: 12319 SIG: 9

Upvotes: 3

Views: 3004

Answers (1)

MH.
MH.

Reputation: 45493

Change the following line:

final Dialog dialog = new Dialog(getActivity().getApplicationContext());

to:

final Dialog dialog = new Dialog(getActivity());

Dialogs cannot be instantiated with the application context as the two have a different life cycle: the dialog's life cycle is bound to that of the hosting Activity. A simple search here on SO would probably have brought that up too.

For an explanation of the different Context flavours and when (not) to use which one, I'd recommend a read through possible mobile's blog post on this topic. It also contains a handy table for reference.

Upvotes: 9

Related Questions