2

我做了很多搜索以找到答案,但仍然不知道我做错了什么。我只是尝试将 AutoCompleteTextView 与动态数组适配器一起使用。但这对我不起作用:(

我的 OnCreate 方法如下所示:

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.travel_dest,(ViewGroup) findViewById(R.layout.statepanel));
pw = new PopupWindow(layout, 350, 500, true);
List<CharSequence> lastTravels = new ArrayList<CharSequence>();
lastTravels.add("ITT");     
adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item, lastTravels);
textView = (AutoCompleteTextView) layout.findViewById(R.id.autoCompleteTextView1);
textView.setAdapter(adapter);       
textView.addTextChangedListener(this);
textView.setThreshold(1);

我的 afterTextChanged 实现:

适配器。添加(“你好”);

调用“add”方法时出现异常:

> android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@2b074218 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:546)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:911)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:823)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1210)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1022)
at android.widget.AutoCompleteTextView.access$1700(AutoCompleteTextView.java:92)
at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1670)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)

刚刚尝试过,如果我在 oncreate 方法中调用 adapter.add 我得到了同样的异常

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.travel_dest,(ViewGroup) findViewById(R.layout.statepanel));
pw = new PopupWindow(layout, 350, 500, true);
List<String> lastTravels = new ArrayList<String>();
lastTravels.add("ITT");     
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, lastTravels);
adapter.add("OTT");
textView = (AutoCompleteTextView) layout.findViewById(R.id.autoCompleteTextView1);
textView.setAdapter(adapter);       
textView.addTextChangedListener(this);
textView.setThreshold(1);
textView.showDropDown();
4

1 回答 1

0

我认为问题在于您将视图作为上下文传递。尝试更改new PopupWindow(layout, 350, 500, true)new PopupWindow(this, 350, 500, true)(如果this将是您的活动。

于 2012-05-19T21:23:49.433 回答