无法添加 window -- token null 无效;你是 activity 运行 吗?在自动完成文本视图中

Unable to add window -- token null is not valid; is your activity running? in AutoCompletetextView

我已经打开相机并点击相机图片进入下一个 activity 那时候应该是我的应用程序崩溃了。我不知道是什么 error.please 让我知道这个 error.Am 收到此错误是 autocompletetextviewChnged 错误原因。

    autoexheader.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.length() == 0) {
                autoexheader.showDropDown();
            }


        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.length() == 0) {
                autoexheader.showDropDown();
            }
        }
    });

Below error

 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:965)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:387)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:96)
    at android.widget.PopupWindow.originalInvokePopup(PopupWindow.java:1590)
    at android.widget.PopupWindow.invokePopup(PopupWindow.java:1575)
    at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1426)
    at android.widget.ListPopupWindow.originalShow(ListPopupWindow.java:734)
    at android.widget.ListPopupWindow.show(ListPopupWindow.java:645)
    at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1316)
    at com.ppms.expensemanger.view.Expense.onTextChanged(Expense.java:174)
    at android.widget.TextView.sendOnTextChanged(TextView.java:10572)
    at android.widget.TextView.setText(TextView.java:6299)
    at android.widget.TextView.setText(TextView.java:6124)
    at android.widget.EditText.setText(EditText.java:122)
    at android.widget.TextView.setText(TextView.java:6076)
    at android.widget.TextView.onRestoreInstanceState(TextView.java:5943)
    at android.view.View.dispatchRestoreInstanceState(View.java:19935)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3892)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3892)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3892)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3892)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3892)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3892)
    at android.view.View.restoreHierarchyState(View.java:19913)
    at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2162)
    at android.app.Activity.onRestoreInstanceState(Activity.java:1602)
    at android.app.Activity.performRestoreInstanceState(Activity.java:1557)
    at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1354)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3351)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2047)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

在向用户显示 activity 之前添加了一些对话框或其他类型的 window,因此没有可用于添加新 window 的令牌,请尝试使用post 在您看来,它可能会帮助您解决错误。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    autoexheader.post(new Runnable() {
        @Override
        public void run() {

            autoexheader.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 0) {
                    autoexheader.showDropDown();
                }


            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() == 0) {
                    autoexheader.showDropDown();
                }
            }
        });

        }
    });