gpt4 book ai didi

android - BadTokenException 在 ArrayAdapter 添加

转载 作者:行者123 更新时间:2023-11-29 13:58:49 27 4
gpt4 key购买 nike

我进行了大量搜索以找到答案,但仍然不知道我做错了什么。我只是尝试将 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 实现:

adapter.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();

最佳答案

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

关于android - BadTokenException 在 ArrayAdapter 添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668880/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com