0

我正在尝试ListView在 android 应用程序中使用过滤器,但没有这样做。到目前为止,这是我的代码:

inputSearch.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
        mHistoryAdapter.getFilter().filter(s.toString());
    }

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

    @Override
    public void afterTextChanged(Editable s) {
    }
});
@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    if (planetFilter == null)
        planetFilter = new PlanetFilter();
    return null;
}

private class PlanetFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if (constraint == null || constraint.length() == 0) {
            results.values = arraylist;
            results.count = arraylist.size();
        } else {
            ArrayList<HashMap<String,Object>> arraylistmenu
                = new ArrayList<HashMap<String,Object>>();
            String filterString = constraint.toString().toLowerCase();
            HashMap<String, Object> filterableString;
            for(int i = 0; i<arraylist.size(); i++){
                filterableString = arraylist.get(i);
                if(filterableString.toString().contains(filterString)){
                    arraylistmenu.add(filterableString);
                }
            }
            results.values = arraylistmenu;
            results.count = arraylistmenu.size();
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint,
                                  FilterResults results) {
    }

错误堆栈跟踪:

07-22 16:22:56.617: E/AndroidRuntime(359): FATAL EXCEPTION: main
07-22 16:22:56.617: E/AndroidRuntime(359): java.lang.NullPointerException
07-22 16:22:56.617: E/AndroidRuntime(359):  at com.app.example$2.onTextChanged(example.java:117)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.widget.TextView.handleTextChanged(TextView.java:6172)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:195)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.widget.TextView.doKeyDown(TextView.java:4304)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.widget.TextView.onKeyDown(TextView.java:4149)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.View.dispatchKeyEvent(View.java:3740)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
07-22 16:22:56.617: E/AndroidRuntime(359):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
07-22 16:22:56.617: E/AndroidRuntime(359):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
07-22 16:22:56.617: E/AndroidRuntime(359):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.os.Looper.loop(Looper.java:123)
07-22 16:22:56.617: E/AndroidRuntime(359):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-22 16:22:56.617: E/AndroidRuntime(359):  at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:22:56.617: E/AndroidRuntime(359):  at java.lang.reflect.Method.invoke(Method.java:521)
07-22 16:22:56.617: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-22 16:22:56.617: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-22 16:22:56.617: E/AndroidRuntime(359):  at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

0

你得到NullPointerExceptiononTextChanged()方法。以下代码导致此异常:

mHistoryAdapter.getFilter().filter(s.toString());

可以在这里找到原因:

@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    if (planetFilter == null)
        planetFilter = new PlanetFilter();

        return null;
}

问题是你总是回来null。该Filter对象永远不会返回,设置mHistoryAdapter.getFilter()null.

将方法更改getFilter()为:

    @Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    if (planetFilter == null)
        planetFilter = new PlanetFilter();
    }

    return planetFilter;         
}
于 2013-07-22T11:11:52.327 回答
0

NullPointerException在文件 example.java 中的第 117 行有一个。在该行设置断点并运行调试并检查什么是 NULL :)

于 2013-07-22T11:10:06.777 回答
0

您的getFilter()方法返回null,而不是planetFilter

于 2013-07-22T11:11:10.533 回答
0
public Filter getFilter() {
        // TODO Auto-generated method stub
        if (planetFilter == null)
            planetFilter = new PlanetFilter();

        return null;
    }

为什么

return null;

代替:

return planetFiler;
于 2013-07-22T11:11:34.290 回答