2016-05-19 64 views
0

在我的android應用程序中,有一個SearchView。當我向SearchView添加文本時,它將搜索結果顯示爲下拉菜單。但是對於一些設備,我得到了BadTokenException。我該如何解決這個問題?請幫忙。Android SearchView AutocompleteTextView showdropdown BadTokenException

這裏是我的代碼:

searchView = (SearchView) mRoot.findViewById(R.id.searchViewCustomer); 
    searchView.setIconified(false); 
    searchView.clearFocus(); 


    searchView.setOnQueryTextListener(this); 
    searchView.setOnSuggestionListener(this); 
    mSearchViewAdapter = new SearchFeedResultsAdaptorCustomer(this.getActivity(), R.layout.searchview_customer, null, 
      columns, null, -1000); 
    searchView.setSuggestionsAdapter(mSearchViewAdapter); 
    int autoCompleteTextViewID = getResources().getIdentifier("android:id/search_src_text", null, null); 
    AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) searchView.findViewById(autoCompleteTextViewID); 
    searchAutoCompleteTextView.setThreshold(1); 
    ((searchAutoCompleteTextView)).setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT); 
    ((searchAutoCompleteTextView)).showDropDown(); 

這是我的日誌:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nybsys.tillbox/com.nybsys.tillbox.sales.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3779) 
at android.app.ActivityThread.access$800(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5279) 
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:1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 
at android.view.ViewRootImpl.setView(ViewRootImpl.java:788) 
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:265) 
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1044) 
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:950) 
at android.widget.ListPopupWindow.show(ListPopupWindow.java:646) 
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1108) 
at com.nybsys.tillbox.sales.FragmentOne.onCreateView(FragmentOne.java:124) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) 
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2042) 
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:543) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1181) 
at android.app.Activity.performStart(Activity.java:5277) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)  
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3779)  
at android.app.ActivityThread.access$800(ActivityThread.java:150)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)  
at android.os.Handler.dispatchMessage(Handler.java:99)  
at android.os.Looper.loop(Looper.java:137)  
at android.app.ActivityThread.main(ActivityThread.java:5279)  
at java.lang.reflect.Method.invokeNative(Native Method) 

回答

0

有兩件事情可以嘗試:

1.

new Handler().post(new Runnable() { 
     @Override 
     public void run() { 
      searchAutoCompleteTextView.showDropDown(); 
     } 
    }); 
  • Set a標誌來擴展您可以在onResume()中查看的下拉列表,並在那裏致電showDropDown()