WindowManager$BadTokenException 在 EditText 中出现拼写错误的单词

WindowManager$BadTokenException with misspelled words in EditText

在定义 EditText 时,输入类型 textEditText 出现问题:

<EditText
    android:id="@+id/text_name"
    android:inputType="text"
    android:text="some txt"
    .
    .
    ./>

此处 inputTypetext,文本值为 "some txt",其中包含一个拼写错误的单词 "txt".并且此 EditText 包含在显示为弹出窗口的布局中 window.

现在,当某些 ButtonActivity 中单击并弹出此布局时,单词 txt 被加下划线作为错误单词,并且当专注于 EditText 中的任何其他单词时,键盘正常显示并且没有发生任何错误,但是当专注于拼写错误的单词 txt 时,应用程序崩溃并出现以下异常:

11-09 16:50:02.126: W/dalvikvm(5205): threadid=1: thread exiting with uncaught exception (group=0x40ffc9a8)
11-09 16:50:02.127: W/System.err(5205): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41656d18 is not valid; is your activity running?
11-09 16:50:02.127: W/System.err(5205):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:646)
11-09 16:50:02.127: W/System.err(5205):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
11-09 16:50:02.127: W/System.err(5205):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-09 16:50:02.127: W/System.err(5205):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
11-09 16:50:02.127: W/System.err(5205):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:847)
11-09 16:50:02.128: W/System.err(5205):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:811)
11-09 16:50:02.128: W/System.err(5205):     at android.widget.Editor$PinnedPopupWindow.updatePosition(Editor.java:2207)
11-09 16:50:02.128: W/System.err(5205):     at android.widget.Editor$PinnedPopupWindow.show(Editor.java:2164)
11-09 16:50:02.128: W/System.err(5205):     at android.widget.Editor$SuggestionsPopupWindow.show(Editor.java:2406)
11-09 16:50:02.128: W/System.err(5205):     at android.widget.Editor.showSuggestions(Editor.java:1700)
11-09 16:50:02.128: W/System.err(5205):     at android.widget.Editor.run(Editor.java:1599)
11-09 16:50:02.128: W/System.err(5205):     at android.os.Handler.handleCallback(Handler.java:725)
11-09 16:50:02.129: W/System.err(5205):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 16:50:02.129: W/System.err(5205):     at android.os.Looper.loop(Looper.java:153)
11-09 16:50:02.129: W/System.err(5205):     at android.app.ActivityThread.main(ActivityThread.java:5299)
11-09 16:50:02.129: W/System.err(5205):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:50:02.130: W/System.err(5205):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:50:02.133: W/System.err(5205):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-09 16:50:02.134: W/System.err(5205):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-09 16:50:02.134: W/System.err(5205):     at dalvik.system.NativeStart.main(Native Method)

为了修复这个错误,我必须将 EditText 中的 inputType 更改为 textNoSuggestions

那么,为什么会出现这个问题呢?以及如何将 inputType 用作 text 并毫无问题地处理拼写错误的单词?


这就是我以编程方式处理它的方式:

private static PopupWindow pw;
private View layout;
private static ViewGroup vg = (ViewGroup) findViewById(R.id.popup_container);

private void initiate_popup(){
    layout = inflater.inflate(R.layout.popup,
            vg);
    pw = new PopupWindow(layout, 500,
            450, true);
    pw.setOutsideTouchable(true);
    pw.setBackgroundDrawable(new ColorDrawable(
            android.graphics.Color.TRANSPARENT));
    pw.setTouchInterceptor(on_outside_touch);
    pw.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss() {
            hideKeyboard();
        }
    });

    // display the popup in the center
    pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

    String name = "some txt";

    // Declaring EditText
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    final EditText text_name = (EditText) layout.findViewById(R.id.text_name);
    imm.hideSoftInputFromWindow(text_name.getWindowToken(), 0);
    text_name.setText(name);
}

// hide Keyboard method
public void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) ctx
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (imm.isActive()) {
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
    } else {
        ctx.getWindow()
                .setSoftInputMode(
                        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}

我通过更改文本的 inputType 解决了这个问题,而不是 text 我将其更改为 textNoSuggestions 甚至不会检查输入的单词是否正确, 因此应用程序在聚焦时不会崩溃。

因此,文本将定义为:

<EditText
    android:id="@+id/text_name"
    android:inputType="textNoSuggestions"
    android:text="some txt"
    .
    .
    ./>