Android中PopupWindow中的AutoCompleteTextview

时间:2014-06-27 14:31:32

标签: java android popupwindow autocompletetextview

我正在尝试在弹出窗口中实现自动完成textview,我正在尝试找到以下错误的解决方案:

06-27 16:54:19.908: E/AndroidRuntime(11236): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@426d6eb0 is not valid; is your activity running?
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.widget.PopupWindow.invokePopup(PopupWindow.java:1019)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:925)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.widget.ListPopupWindow.show(ListPopupWindow.java:625)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:971)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:953)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.os.Looper.loop(Looper.java:136)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at android.app.ActivityThread.main(ActivityThread.java:5001)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at java.lang.reflect.Method.invoke(Method.java:515)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-27 16:54:19.908: E/AndroidRuntime(11236):    at dalvik.system.NativeStart.main(Native Method)

我发现这两个问题存在同样的问题,但没有解决方案:

Autocomplete TextView in PopupWindow

autocompletetextview not working inside popup window

下面是我的代码:

LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.incomepopup, null);
popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

String[] incomedescstring = getResources().getStringArray( R.array.incomedesclist);
incomedescadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, incomedescstring);
incomeeditdesc = (AutoCompleteTextView) popupView.findViewById(R.id.income_desc);
incomeeditdesc.setAdapter(incomedescadapter);
incomeeditdesc.setThreshold(1);

//Add Button
Button add = (Button) popupView.findViewById(R.id.income_btn_add);
add.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        incomeedit = (EditText) popupView.findViewById(R.id.income_text);
        items.add(new IncomeListViewItem() {
            {
                //Add Item Code
            }
        });
        incomelvadapter.notifyDataSetChanged();
        popupWindow.dismiss();
    }
});

//Dismiss Button
Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        popupWindow.dismiss();
    }
});
popupWindow.showAtLocation(getActivity().getWindow().getDecorView() , Gravity.TOP, 0, 0);

这是incomepopup.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/darker_gray"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/income_text"
            android:layout_width="240dp"
            android:layout_height="50dp"
            android:hint="@string/income_hint"
            android:inputType="phone" />

        <AutoCompleteTextView
            android:id="@+id/income_desc"
            android:layout_width="240dp"
            android:layout_height="50dp"
            android:hint="@string/edit_description" />


        <Button
            android:id="@+id/income_btn_add"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button_add" />

        <Button
            android:id="@+id/dismiss"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/dismiss" />
    </LinearLayout>

</LinearLayout>

问题是弹出窗口打开后,用户在autocompleteTextView中输入数据,应用程序崩溃,我得到上一个错误。

0 个答案:

没有答案