0

I am working on android application in which i want to make a selection dialog box on my imageView click listener. Without dialog box it is working fine, but when i add dialog box with list in it it shows exception. My code and error stack is given below:

profileImage.setOnClickListener(new View.OnClickListener() {
               //@Override
               public void onClick(View v) {
                   Toast.makeText(getApplicationContext(), "ImageClicked",
                            Toast.LENGTH_SHORT).show();     

                   final CharSequence[] items = {"Red", "Green", "Blue"};
                   AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                   builder.setTitle("Pick a color");
                   builder.setCancelable(false);
                 //  builder.setPositiveButton(R.string.dialog_action_dismiss, null);

                   // creating a single choice item menu (radio buttons list)
                   // -1 indicates that no item should be selected by default
                   // pass index argument starting from 0 to preselect an item if required
                   builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int item) {
                       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                     }
                   });

                   AlertDialog alert = builder.create();
                   alert.show();

               }        
            });

11-25 20:03:02.625: E/AndroidRuntime(17343): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-25 20:03:02.625: E/AndroidRuntime(17343):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:650)
11-25 20:03:02.625: E/AndroidRuntime(17343):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
11-25 20:03:02.625: E/AndroidRuntime(17343):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-25 20:03:02.625: E/AndroidRuntime(17343):    at android.app.Dialog.show(Dialog.java:281)
Usman Khan
  • 3,739
  • 6
  • 41
  • 89
  • This is answer to your question: http://stackoverflow.com/questions/5796611/dialog-throwing-unable-to-add-window-token-null-is-not-for-an-application-wi – Dmitriy Puchkov Nov 25 '14 at 15:12

1 Answers1

1

Change getApplicationContext() to v.getContext()

petey
  • 16,914
  • 6
  • 65
  • 97
  • 1
    also, this answer will help you get a valid context from a `DialogInterface`, http://stackoverflow.com/a/22049950/794088 – petey Nov 25 '14 at 15:14
  • 1
    thanks for your reply. It works. But it locks my screen, like when i press back button or if i press any point on the screen nothing happens and it will not allow me to go back on the screen. – Usman Khan Nov 25 '14 at 15:18
  • 1
    does it still do this if you use `builder.setCancelable(true);` – petey Nov 25 '14 at 15:21
  • 1
    where should i use it in my code? I mean on which portion? – Usman Khan Nov 25 '14 at 15:22
  • 1
    where you have `builder.setCancelable(false);`, change it to true. – petey Nov 25 '14 at 15:23