2012-12-23 43 views
0

我試圖使IME服務(屏幕鍵盤)中沒有任何活動的彈出窗口出現。當我調用popUp.showAtLocation(佈局,Gravity.CENTER,0,-100)時,我得到一個「Windowmanager $ BadTokenException:無法添加窗口 - 標記null無效。」我知道從沒有相關活動的服務中打開一個彈出窗口有點不尋常 - 這可能嗎?從IME創建PopupWindow:BadTokenException

這裏是我的代碼:

public void initiatePopupWindow() 
{ 
    try { 

     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.popup_layout,null); 

     // create a 300px width and 470px height PopupWindow 
     popUp = new PopupWindow(layout, 300, 470, true); 
     popUp.showAtLocation(layout, Gravity.CENTER, 0, -100); 


     Button cancelButton = (Button) layout.findViewById(R.id.popup_cancel_button); 
     cancelButton.setOnClickListener(inputView.cancel_button_click_listener); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

任何幫助,將不勝感激。謝謝

回答

0

我發現這個問題 - 我試圖使用佈局繪製一個彈出窗口,並使用與父窗口相同的佈局。解決方案是將父級設置爲另一個視圖。我發現它也是重要的,以確保在創建彈出窗口之前創建了視圖(例如,通過使用處理程序/ runnable)。

public void initiatePopupWindow() 
{ 
    try { 
     Log.i("dotdashkeyboard","initiatePopupWindow (from IME service)"); 
     LayoutInflater inflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.popup_layout,null); 

     // create a 300px width and 470px height PopupWindow 
     popUp = new PopupWindow(layout, 300, 470, false); 
     popUp.showAtLocation(inputView, Gravity.CENTER, 0, -100); 

     Button cancelButton = (Button) layout.findViewById(R.id.popup_cancel_button); 
     cancelButton.setOnClickListener(inputView.cancel_button_click_listener); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}