弹出窗口中的自动完成下拉框会导致 Android 应用程序强制关闭。为什么?

发布于 2024-11-28 20:00:39 字数 3213 浏览 2 评论 0原文

我有一个自动完成框,我试图在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文