-1

我正在尝试TextWatcherEditText. 我EditText在我的应用程序中使用了一个并通过TextWatcher.

当我在栏中键入像“a”这样的字母时,EditText我的光标可以工作,但是当我写下一个像“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)
4

2 回答 2

2

我的理解start是,第一点s是有变化。尽量不要检查是否start为 0

于 2013-01-15T06:07:50.347 回答
1

here's my code..

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;
    }
于 2013-01-15T05:40:59.480 回答