我想對EditText
實施TextWatcher
。我在我的應用中使用了EditText
,並通過TextWatcher
觀看。錯誤的TextWatcher中的EditText的Android
當我在EditText
欄中鍵入字母「a」時,我的光標可以正常工作,但是當我寫下「ab *」等下一個字母表時,光標根本不起作用,並且在刪除這些字母時發生了錯誤,並且我的應用崩潰了。我無法弄清楚我錯過了什麼。
這裏是我的代碼:
TextWatcher myTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (start == 0) {
SearchWordString.clear();
Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
new String[] {(search.getText().toString())}, null);
while(cursor.isAfterLast()==false)
{
SearchWordString.add(cursor.getString(1));
cursor.moveToNext();
}
listAdapter.notifyDataSetChanged();
viewFlipper.showNext();
} else {
}
listAdapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
這是我的logcat
01-15 04:54:25.080: W/dalvikvm(29324): threadid=1: thread exiting with uncaught exception (group=0x40b811f8)
01-15 04:54:25.103: E/AndroidRuntime(29324): FATAL EXCEPTION: main
01-15 04:54:25.103: E/AndroidRuntime(29324): java.lang.NullPointerException
01-15 04:54:25.103: E/AndroidRuntime(29324): at com.ilmasoft.GPD.Start$2.onTextChanged(Start.java:1432)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.widget.TextView.sendOnTextChanged(TextView.java:8085)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.widget.TextView.handleTextChanged(TextView.java:8132)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8495)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:389)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:216)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:29)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:90)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:45)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:151)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:352)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.widget.TextView.doKeyDown(TextView.java:5716)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.widget.TextView.onKeyDown(TextView.java:5531)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.KeyEvent.dispatch(KeyEvent.java:2608)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.View.dispatchKeyEvent(View.java:5547)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
01-15 04:54:25.103: E/AndroidRuntime(29324): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2021)
01-15 04:54:25.103: E/AndroidRuntime(29324): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1382)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
01-15 04:54:25.103: E/AndroidRuntime(29324): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1948)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3360)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2618)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.os.Looper.loop(Looper.java:137)
01-15 04:54:25.103: E/AndroidRuntime(29324): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-15 04:54:25.103: E/AndroidRuntime(29324): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 04:54:25.103: E/AndroidRuntime(29324): at java.lang.reflect.Method.invoke(Method.java:511)
01-15 04:54:25.103: E/AndroidRuntime(29324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-15 04:54:25.103: E/AndroidRuntime(29324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-15 04:54:25.103: E/AndroidRuntime(29324): at dalvik.system.NativeStart.main(Native Method)
什麼是線... *畏縮* ... Start.java的1432? – Eric
從這一行我的循環開始從光標讀取,它在代碼中給出。 (cursor.isAfterLast()== false)\t \t \t \t SearchWordString.add(cursor.getString(1)); \t \t \t \t cursor.moveToNext(); } – User42590
假設你的意思是帶有'while()'的行,'cursor'爲空,這可能意味着你在查詢的輸入中有錯誤。 – Eric