Artem
Artem

Reputation: 4639

Not open list autotextcompleteview

I want open drop down list when my Activity started. I use this code:

 mAutoCompleteTextView.requestFocus();
 mAutoCompleteTextView.showDropDown();

mAutoCompleteTextView not null.

I catch this exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.DriverNotes.AndroidMobileClient/com.DriverNotes.AndroidMobileClient.RefillActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
        at android.app.ActivityThread.access$800(ActivityThread.java:142)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:566)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.widget.PopupWindow.invokePopup(PopupWindow.java:1071)
        at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:977)
        at android.widget.ListPopupWindow.show(ListPopupWindow.java:641)
        at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096)
        at com.DriverNotes.AndroidMobileClient.RefillActivity.initFields(RefillActivity.java:792)
        at com.DriverNotes.AndroidMobileClient.RefillActivity.onCreate(RefillActivity.java:171)

Upvotes: 2

Views: 1060

Answers (1)

Artem
Artem

Reputation: 4639

I catch this exception, becouse I try call showDropDown() in onCreate() method. In this method Activity not started and AutoCompleteTextView can't attach to Acivity. I use this code for open list with items in AutoCompletetextView:

mAutoCompleteTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                mAutoCompleteTextView.showDropDown();
            }
        }
    });

mAutoCompletetextView get focus when actiity has been started.

Upvotes: 4

Related Questions