android.view.WindowManager $ BadTokenException:无法添加窗口?

时间:2015-07-20 18:47:46

标签: android android-layout

我想在我的活动中显示一个弹出菜单,所以我在onCreate上调用popup方法,但是我得到了这个例外。可以帮忙吗?我无法理解,我是java和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)

显示弹出窗口的方法

 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();
    }
}

1 个答案:

答案 0 :(得分:1)

现在创建PopUp Windows onCreate还为时过早。即使你在

之后也这样做了
setContentView( R.layout.activity_main );

语句。你必须在一秒钟之后再做。你可以用它。

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

这是一个例子。我不知道你的代码是如何工作的。但是你必须给Android OS一些时间来创建和初始化视图。 setContentView() 方法调用并不意味着完全创建了视图。

编辑:如果您在片段 onCreate() 中创建PopUp,这也是错误的。因为片段的视图已在 onActivityCreated() 方法中完全初始化。您只需在片段内调用PopUp初始化方法 onActivityCreated()