在我的Android应用程序中,有一个SearchView。当我向SearchView添加文本时,它会将搜索结果显示为下拉列表。但是对于某些设备我得到了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 :(得分:0)
你可以尝试两件事:
1
new Handler().post(new Runnable() {
@Override
public void run() {
searchAutoCompleteTextView.showDropDown();
}
});
onResume()
中查看的下拉列表,并在那里调用showDropDown()
。