开发者

Autocomplete drop-down box in popup causes Android app to force close. Why?

开发者 https://www.devze.com 2023-03-26 21:13 出处:网络
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

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 开发者_JAVA技巧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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号