Usman Khan
Usman Khan

Reputation: 3953

Alert Dialogue box on image view click listener in android

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)

Upvotes: 0

Views: 2088

Answers (1)

petey
petey

Reputation: 17140

Change getApplicationContext() to v.getContext()

Upvotes: 1

Related Questions