2013-01-15 52 views
-1

我想對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) 
+0

什麼是線... *畏縮* ... Start.java的1432? – Eric

+1

從這一行我的循環開始從光標讀取,它在代碼中給出。 (cursor.isAfterLast()== false)\t \t \t \t SearchWordString.add(cursor.getString(1)); \t \t \t \t cursor.moveToNext(); } – User42590

+0

假設你的意思是帶有'while()'的行,'cursor'爲空,這可能意味着你在查詢的輸入中有錯誤。 – Eric

回答

2

這是我的理解是start是th e在s的第一個點有變化。儘量不要做一個檢查,看看是否start是0

1

這裏是我的代碼..

mEdtSearchCity = (AutoCompleteTextView) findViewById(R.id.edtSearchCity); 

mEdtSearchCity.addTextChangedListener(this); 

public class SearchCityListActivity extends Activity implements OnItemClickListener,TextWatcher 

@Override 
    public void afterTextChanged(Editable s) { 
     // TODO Auto-generated method stub 
     String cityName = mEdtSearchCity.getText().toString(); 
     cityName = mEdtSearchCity.getText().toString(); 
     Log.d(TAG, "text changed "+cityName); 
     if (!(cityName.equals(""))){ 
     lookupTimeZone(cityName); 
     }else{ 
      adapter.clear(); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
private void lookupTimeZone(String s){ 
     Log.d(TAG, "cityname "+s); 
     ArrayList<LocationVO> alist = null; 
     alist = lookup.getTimeZoneForCity(s); 
     Log.d(TAG, "lookup list "+alist.size()); 

     if (alist.size() != 0 ){ 
      //arrayCity.clear(); 
      adapter.clear(); 
      LocationVO lo = new LocationVO(); 
      for (int i = 0; i < alist.size(); i++) { 
       lo = alist.get(i); 
       searchResults.add(lo); 
      } 
      adapter.notifyDataSetChanged(); 
     }else { 
      //Toast.makeText(SearchCityListActivity.this, "No results found", Toast.LENGTH_SHORT).show(); 
      Log.d(TAG, "No data"); 
      adapter.clear(); 

     } 

public ArrayList<LocationVO> getTimeZoneForCity(String s){ 
     Cursor c ; 
     try{ 
     String city = s; 
     if (city.contains("'")){ 
      Log.d(TAG, "matched"); 
      String [] as = city.split("'"); 
      Log.d(TAG, "st "+as[0]+"''"+as[1]); 
      c = theDatabase.query("data", new String[] {KEY_CITY, KEY_TIMEZONE, KEY_COUNTRY, KEY_TIMEZONE_DISPLAY_NAME, KEY_TIMEZONE_ID},KEY_CITY+" like '"+as[0]+"''"+as[1]+"%';", null, null, null, null, null); 
     }else{ 
     c = theDatabase.query("data", new String[] {KEY_CITY, KEY_TIMEZONE, KEY_COUNTRY, KEY_TIMEZONE_DISPLAY_NAME, KEY_TIMEZONE_ID},KEY_CITY+" like '"+city+"%';", null, null, null, null, null); 
     Log.d(TAG, "Query:- "+c.toString()); 
     } 
     }catch(Exception e){ 
      Log.d(TAG, "Error getting Timezone for "+KEY_CITY+" : "+e.getMessage()); 
      ArrayList<LocationVO> a = new ArrayList<LocationVO>(); 
      return a; 
     } 

     int count = c.getCount(); 
     Log.d(TAG, "count "+count); 

     if(count == 0){ 
      ArrayList<LocationVO> list = new ArrayList<LocationVO>(); 
      Log.d(TAG, "null list"+list); 
      return list; 
     } 
     Log.d(TAG, "outside bound"); 
     c.moveToFirst(); 

     ArrayList<LocationVO> list = new ArrayList<LocationVO>(); 
     for(int i=0; i<count; i++){ 

      LocationVO vo = new LocationVO(); 
      vo.cityName = c.getString(0); 
      vo.countryName = c.getString(2); 
      vo.setTimezoneString(c.getString(1)); 
      vo.setTimeZoneDisplayName(c.getString(3)); 
      vo.setTimeZoneId(c.getString(4)); 
      list.add(vo); 
      c.moveToNext(); 
     } 

     c.close(); 
     return list; 
    } 
+1

能你請在上面的代碼中找出錯誤?根據給定的問題給出解決方案? – User42590

+0

在其Android版本當U輸入AB *像串..這meight被職高becouse的* ..沒有ü嘗試沒有* entring字符串你使用此代碼.. – NaserShaikh

+1

3.0及以上版本的Android – User42590

相關問題