2017-05-31 83 views
0

我不能在popupwindow裏面過濾列表視圖android-在popupwindow裏面過濾listview

它失敗,異常

android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:679) 
     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) 
     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) 
     at android.widget.PopupWindow.invokePopup(PopupWindow.java:1378) 
     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1154) 
     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1121) 
     at android.widget.AbsListView.positionPopup(AbsListView.java:5665) 
     at android.widget.AbsListView.showPopup(AbsListView.java:5651) 
     at android.widget.AbsListView.onTextChanged(AbsListView.java:6095) 
     at android.widget.TextView.sendOnTextChanged(TextView.java:8187) 
     at android.widget.TextView.setText(TextView.java:4483) 
     at android.widget.TextView.setText(TextView.java:4337) 
     at android.widget.EditText.setText(EditText.java:89) 
     at android.widget.TextView.setText(TextView.java:4312) 
     at android.widget.AbsListView.setFilterText(AbsListView.java:1944) 

我顯示PopupWindow爲:

popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0); 

彈出佈局是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <EditText 
     android:hint="" /> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </ListView> 
</RelativeLayout> 

你能提供指針?

+0

提供適用於listview的適配器代碼? –

+0

我解決了這個問題,通過使用對話框而不是popupwindow – GJain

回答

0

我認爲問題在於您顯示的彈出窗口太早顯示。在runnable中使用showatlocation,如下所示

new Handler().postDelayed(new Runnable(){ 
    public void run() { 
     pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 
    } 
}, 100L); 
+0

問題是彈出窗口內的列表過濾。我能夠顯示彈出。但是,當列表過濾發生時,它會嘗試顯示另一個彈出窗口,引發異常。 – GJain

+0

嘗試使用runnable,如果它工作或發佈您的代碼,將找出問題。 –