1

拼错的单词我有一个问题与的inputType textEditText定义EditText像下面这样的时候:

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

这里inputTypetext,文本价值"some txt"其中包含一个拼写错误的词,它是“txt”。这个EditText包含在一个布局中,该布局显示为一个弹出窗口。

现在,当一些Button点击了Activity这个布局POP操作机,字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 [email protected] 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$1.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) 

并修复这个bug我必须改变inputTypeEditTexttextNoSuggestions

那么,为什么会发生这个问题呢?以及如何使用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); 
    } 
} 

回答

0

我通过更改文本的inputType解决了这个问题,而不是text我改成了textNoSuggestions将连看都不看,如果输入的单词是否正确,因此应用程序在聚焦时不会崩溃。

所以,文本将定义为:

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