弹出窗口中的自动完成下拉框会导致 Android 应用程序强制关闭。为什么?
我有一个自动完成框,我试图在我的 Android 应用程序的弹出窗口中实现它。将此弹出框放入我的主要活动布局中效果很好。当我将其移动到弹出布局时,它将成功显示在屏幕上。但是,当我输入超出自动完成阈值并找到匹配项(从而触发与我的部分文本匹配的项目的下拉列表)时,应用程序崩溃并出现以下错误:
08-08 11:12:53.803 E/AndroidRuntime(22221): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@4057f5c0 is not valid; is your activity running?
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.ViewRoot.setView(ViewRoot.java:544)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.Window$LocalWindowManager.addView(Window.java:430)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.PopupWindow.invokePopup(PopupWindow.java:916)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:828)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1277)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1081)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1064)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.os.Looper.loop(Looper.java:143)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.app.ActivityThread.main(ActivityThread.java:4196)
08-08 11:12:53.803 E/AndroidRuntime(22221): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 11:12:53.803 E/AndroidRuntime(22221): at java.lang.reflect.Method.invoke(Method.java:507)
08-08 11:12:53.803 E/AndroidRuntime(22221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-08 11:12:53.803 E/AndroidRuntime(22221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-08 11:12:53.803 E/AndroidRuntime(22221): at dalvik.system.NativeStart.main(Native Method)
这是我使用的代码(弹出窗口在单击按钮,该按钮位于活动的主布局中):
public void onClick(View v) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.auto_complete_location, null, false),
400, 600, true);
AutoCompleteTextView autoCompleteLocationsView =(AutoCompleteTextView)
pw.getContentView().findViewById(R.id.autoCompleteLocations);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(autoCompleteLocationsView.getContext(),
android.R.layout.simple_dropdown_item_1line, locationNames);
autoCompleteLocationsView.setAdapter(adapter);
pw.showAtLocation(findViewById(R.id.mainActivityContainer), Gravity.CENTER, 0, 0);
}
提前感谢您的任何帮助。
I have an auto complete box which I am trying to implement in a popup window in my Android app. Putting this popup box in my main activity layout works fine. When I moved it to my popup layout, it will successfully display on the screen. However, when I type beyond the autocomplete threshhold and a match is found (thus triggering the drop down list of items which match my partial text) the application crashes with the following error:
08-08 11:12:53.803 E/AndroidRuntime(22221): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@4057f5c0 is not valid; is your activity running?
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.ViewRoot.setView(ViewRoot.java:544)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.view.Window$LocalWindowManager.addView(Window.java:430)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.PopupWindow.invokePopup(PopupWindow.java:916)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:828)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1277)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1081)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1064)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.os.Looper.loop(Looper.java:143)
08-08 11:12:53.803 E/AndroidRuntime(22221): at android.app.ActivityThread.main(ActivityThread.java:4196)
08-08 11:12:53.803 E/AndroidRuntime(22221): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 11:12:53.803 E/AndroidRuntime(22221): at java.lang.reflect.Method.invoke(Method.java:507)
08-08 11:12:53.803 E/AndroidRuntime(22221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-08 11:12:53.803 E/AndroidRuntime(22221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-08 11:12:53.803 E/AndroidRuntime(22221): at dalvik.system.NativeStart.main(Native Method)
And here is the code I use (the popup is launched on button click, where the button is located in main layout of the activity):
public void onClick(View v) {
LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.auto_complete_location, null, false),
400, 600, true);
AutoCompleteTextView autoCompleteLocationsView =(AutoCompleteTextView)
pw.getContentView().findViewById(R.id.autoCompleteLocations);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(autoCompleteLocationsView.getContext(),
android.R.layout.simple_dropdown_item_1line, locationNames);
autoCompleteLocationsView.setAdapter(adapter);
pw.showAtLocation(findViewById(R.id.mainActivityContainer), Gravity.CENTER, 0, 0);
}
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论