Jack
Jack

Reputation: 1855

android.view.WindowManager$BadTokenException: Unable to add window?

I want to show a popup menu in my activity,so I am calling the popup method on onCreate but I got this exception.Can anybody help? I could not understand ,I am a begginer to java and android.

   android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:700)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
        at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
        at android.view.Window$LocalWindowManager.addView(Window.java:554)
        at android.widget.PopupWindow.invokePopup(PopupWindow.java:1013)
        at android.widget.PopupWindow.showAtLocation(PopupWindow.java:856)
        at android.widget.PopupWindow.showAtLocation(PopupWindow.java:820)
        at project1.me.com.cookbookintent.ImageViewActivity.initiatePopupWindow(ImageViewActivity.java:271)
        at project1.riafy.com.cookbookintent.ImageViewActivity.onCreate(ImageViewActivity.java:91)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)

Method to show Popup window

 public void initiatePopupWindow() {
    try {

        LayoutInflater inflater;
        inflater = (LayoutInflater) ImageViewActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupmenu, (ViewGroup) findViewById(R.id.popup_element));
        pwindo = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

        // pwindo.setFocusable(true);
        pwindo.setOutsideTouchable(true);
        //  pwindo.showAsDropDown();
        Button btnStopUpload = (Button) layout.findViewById(R.id.btn_upload);
        btnStopUpload.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
              //  mNotifyManager.cancel(MY_NOTIFICATION_ID);
                //  mTask.cancel(true);
                Log.e(TAG, "Notification Cancelled ");
                // mTask.cancel(true);
              /*  Toast.makeText(getApplicationContext(),
                        "Upload Cancelled", Toast.LENGTH_SHORT).show();*/
                Intent i=new Intent(ImageViewActivity.this,MyService.class);
                //stopService(i);
                ImageViewActivity.this.stopService(i);
                ImageViewActivity.this.finish();
            }
        });


        Button btnCancelPopup = (Button) layout.findViewById(R.id.btn_cancel);

        btnCancelPopup.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                pwindo.dismiss();

            }
        });
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "POPUP Rejected", Toast.LENGTH_SHORT).show();
        Log.e("COOKBOOK", "I got an error", e);
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 1578

Answers (1)

Metehan Toksoy
Metehan Toksoy

Reputation: 1935

It is too early to create PopUp Windows onCreate. Even you do it after

setContentView( R.layout.activity_main );

statement. You have to do it a second later. You can use this.

new Handler(  ).postDelayed( new Runnable() {
        @Override
        public void run() {
            initiatePopupWindow();
        }
    }, 1000 );

This is example. I don't know how your code works. But you have to give a time to Android os to create and initialize views. setContentView() method call doesn't mean views are completely created.

EDIT: If you create PopUp in fragment's onCreate(), this is wrong too. Because fragment's view are completely initialized at onActivityCreated() method. You can just call your PopUp initializer method onActivityCreated() inside the fragment.

Upvotes: 1

Related Questions