在Android中使用TextChanged对EditText中的字母计数失败?

时间:2014-02-23 16:18:57

标签: android android-edittext

我在这里编写了代码,但它的应用程序正在破碎。实际上我已经把“用户”使用的EditText输入到搜索内容中。如果书面字母大于3,则将调用搜索功能。目前它崩溃了。

代码:

     searchEditText = (EditText)findViewById(R.id.search_edittext);

    searchFatwaListView = (ListView)findViewById(R.id.search_fatwa_list);

         searchEditText.addTextChangedListener(new TextWatcher() {
             public void afterTextChanged(Editable s)
             {
             }

             public void beforeTextChanged(CharSequence s, int start, int count, int after)
             {
             }

             public void onTextChanged(CharSequence s,int start, int before, int count)
             {
                if(s.length() == 3 || s.length() > 3)
                {
                    Log.i("if block","search fatwa"+s);
                    Log.i("if block","search fatwa"+searchEditText.getText().toString());
                    getFatwaSearchContents(s.toString());
                }
                else
                {
                    Log.i("else block","search fatwa"+searchEditText.getText().toString() +"::count::"+count);
                }
             }
             });
    public void getFatwaSearchContents(String editTextString)
{
    ArrayList<FatwaBean> fatwaImageList = AndroidUtil.externalDb.getSearchFatwaData(editTextString);

    Log.i("questionImagesNameList:",""+fatwaImageList + ":::id:"
            + fatwaImageList);
    if(fatwaImageList.size()>0)
    {
    for (int i = 0;i < fatwaImageList.size();i++) {
        Log.i("fatwaImageList",""+fatwaImageList.get(i).getFatwaImageName().toString());
        //searchFatwaListView.setAdapter(new CustomAdapterClass(fatwaImageList,context));
        }   
    }
    else
    {
Log.i("No search Contents Found",""+fatwaImageList.size());
    }   
}

错误

    02-23 22:26:56.069: E/AndroidRuntime(474): FATAL EXCEPTION: main
02-23 22:26:56.069: E/AndroidRuntime(474): java.lang.NullPointerException
02-23 22:26:56.069: E/AndroidRuntime(474):  at com.example.jamiaduplicate.SearchFatwaActivity.getFatwaSearchContents(SearchFatwaActivity.java:79)
02-23 22:26:56.069: E/AndroidRuntime(474):  at com.example.jamiaduplicate.SearchFatwaActivity$1.onTextChanged(SearchFatwaActivity.java:59)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.widget.TextView.sendOnTextChanged(TextView.java:6784)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.widget.TextView.handleTextChanged(TextView.java:6824)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6973)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:901)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:356)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:270)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:436)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:413)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:218)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.widget.TextView.doKeyDown(TextView.java:4761)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.widget.TextView.onKeyDown(TextView.java:4581)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.KeyEvent.dispatch(KeyEvent.java:2335)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.View.dispatchKeyEvent(View.java:4569)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1113)
02-23 22:26:56.069: E/AndroidRuntime(474):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1692)
02-23 22:26:56.069: E/AndroidRuntime(474):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1260)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.app.Activity.dispatchKeyEvent(Activity.java:2237)
02-23 22:26:56.069: E/AndroidRuntime(474):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1648)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2682)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2655)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1952)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.os.Looper.loop(Looper.java:126)
02-23 22:26:56.069: E/AndroidRuntime(474):  at android.app.ActivityThread.main(ActivityThread.java:3997)
02-23 22:26:56.069: E/AndroidRuntime(474):  at java.lang.reflect.Method.invokeNative(Native Method)
02-23 22:26:56.069: E/AndroidRuntime(474):  at java.lang.reflect.Method.invoke(Method.java:491)
02-23 22:26:56.069: E/AndroidRuntime(474):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-23 22:26:56.069: E/AndroidRuntime(474):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-23 22:26:56.069: E/AndroidRuntime(474):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案