2012-01-08 81 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

相關問題