2012-01-08 50 views
43

显示PopupWindow时出现以下错误。 的错误是由行触发:

checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0); 

MapView的是MapView的,没有什么是零。 堆栈跟踪:

01-08 18:00:09.402: E/AndroidRuntime(27768): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.ViewRootImpl.setView(ViewRootImpl.java:513) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.view.Window$LocalWindowManager.addView(Window.java:537) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.widget.PopupWindow.invokePopup(PopupWindow.java:988) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:845) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at com.geoloc.ActivityCheckIn.onCreate(ActivityCheckIn.java:50) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.app.Activity.performCreate(Activity.java:4465) 
01-08 18:00:09.402: E/AndroidRuntime(27768): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 

这是从我的活动代码(扩展MapActivity)

protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.checkin); 
    mapView = (MapView) findViewById(R.id.mapview); 

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    checkInPopup = new PopupWindow(inflater.inflate(CHECK_IN_POPUP_LAYOUT, null, false)); 
    checkInPopup.setOutsideTouchable(true); 
    checkInPopup.setHeight(100); 
    checkInPopup.setWidth(200); 
    checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0); 
} 

感谢您分享您的想法

回答

52

你是显示太早您的弹出窗口。您可能会在发布的onResume延迟可运行于showatlocation,试试看吧

编辑: 这篇文章似乎有同样的问题回答Problems creating a Popup Window in Android Activity

+1

它的工作原理弹出。谢谢你指点我正确的方向。 – znat 2012-01-09 01:37:26

+0

Owww mahn,伟大的思维方式........................... – 2017-08-17 21:04:47

2

一个弹出的父母自身不能是一个弹出。他们的父母都必须是一样的。因此,如果您在弹出窗口中创建弹出窗口,则必须保存父窗口的弹出窗口并将其设置为父窗口。

这里是一个example

+2

您能给我们一个代码示例吗? – user2224350 2013-10-16 23:23:30

9

有些情况下可能会出现此异常两种情形。其中一个是nandeesh提到的。另一种情况就是这里所说的:http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/

确保你处理两者

+7

请注意,只有链接的答案是不鼓励的,引用会随着时间的推移而变得陈旧。请考虑在此添加独立的摘要,并将链接保留为参考。 – kleopatra 2013-06-26 07:19:45

+0

@TheMan尼斯〜一个如此小小的强大提示!谢谢! – cmcromance 2013-07-23 00:41:29

22

同样的问题发生了,当我尝试以显示活动的弹出式菜单中我也得到了同样的错误时抛出,但我遇到的问题ñ解决通过提供上下文ActivityName.this代替getApplicationContext()和是它的工作对我来说可能会帮助别人

2
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    View view = LayoutInflater.from(mContext).inflate(R.layout.popup_window_layout, new LinearLayout(mContext), true); 
    popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    popupWindow.setContentView(view); 
} 

    @Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    if (hasFocus) { 
     popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0); 
    } 
} 

正确的做法是在onWindowFocusChanged popupwindow.show()()。

1

尽量展现像下面

findViewById(R.id.main_layout).post(new Runnable() { 
     public void run() { 
      mPopupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0); 
      Button close = (Button) customView.findViewById(R.id.btn_ok); 
      close.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        mPopupWindow.dismiss(); 
        doOtherStuff(); 
       } 
      }); 
     } 
    }); 
0

使用尝试

LayoutInflater inflater = (LayoutInflater).getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
View view = inflate.from(YourActivity.this).inflate(R.layout.yourLayout, null); 
+0

你可能也可以发布完整的代码,以确保他正在替换正确的东西。谢谢。 – kwoxer 2017-02-23 12:29:55

相关问题