简体   繁体   English

搜索列表视图正在检索错误..我不知道为什么

[英]Search Listview is retrieving error..and I don't know why

The code below was working perfectly.下面的代码运行良好。 To be honest I don't even think I changed something.老实说,我什至不认为我改变了什么。 It stopped working.它停止工作。 I have a custom Listview, and the error is on search inside that custom listview.我有一个自定义列表视图,错误是在该自定义列表视图中搜索。 I created a custom Adapter, etc.我创建了一个自定义适配器等。

// Products Activity:
listView = (ListView) findViewById(R.id.product_listview);
inputSearch = (EditText) findViewById(R.id.inputSearch);

adapter = new itemAdapter(this,R.layout.row, display_products);
listView.setAdapter(adapter);

/**
 * Enabling Search Filter
 * */
inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
        adapter.filter(text);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub
    }
});

And this is filter on custom adapter (I didn't put the getView method because it's working..but if you need let me know.):这是自定义适配器上的过滤器(我没有放置 getView 方法,因为它正在工作......但如果你需要让我知道。):

public class itemAdapter extends ArrayAdapter<oc_product_display> {

    private final Context context;
    private final List<oc_product_display> lista;
    private ArrayList<oc_product_display> arraylist;
    private final int rowResourceId;

    public itemAdapter(Context context, int textViewResourceId, List<oc_product_display> objects) {

        super(context, textViewResourceId, objects);
        this.context = context;
        this.lista = objects;
        this.rowResourceId = textViewResourceId;
        this.arraylist = new ArrayList<oc_product_display>();
        this.arraylist.addAll(lista);
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        lista.clear();
        if (charText.length() == 0) {
            lista.addAll(arraylist);
        } else {
            for (oc_product_display wp : arraylist) {
                if (wp.get_name().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    lista.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }
}

LogCat日志猫

  And this is the error: 10-04 06:29:08.576: E/AndroidRuntime(10284):
 FATAL EXCEPTION: main 10-04 06:29:08.576: E/AndroidRuntime(10284):
 java.lang.RuntimeException: Unable to start activity
 ComponentInfo{com.example.myExample/com.example.myExample.ProductsActivity}:
 java.lang.NullPointerException 10-04 06:29:08.576:
 E/AndroidRuntime(10284):   at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
 10-04 06:29:08.576: E/AndroidRuntime(10284):   at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
 10-04 06:29:08.576: E/AndroidRuntime(10284):   at
 android.app.ActivityThread.access$600(ActivityThread.java:141) 10-04
 06:29:08.576: E/AndroidRuntime(10284):     at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
 10-04 06:29:08.576: E/AndroidRuntime(10284):   at
 android.os.Handler.dispatchMessage(Handler.java:99) 10-04
 06:29:08.576: E/AndroidRuntime(10284):     at
 android.os.Looper.loop(Looper.java:137) 10-04 06:29:08.576:
 E/AndroidRuntime(10284):   at
 android.app.ActivityThread.main(ActivityThread.java:5103) 10-04
 06:29:08.576: E/AndroidRuntime(10284):     at
 java.lang.reflect.Method.invokeNative(Native Method) 10-04
 06:29:08.576: E/AndroidRuntime(10284):     at
 java.lang.reflect.Method.invoke(Method.java:525) 10-04 06:29:08.576:
 E/AndroidRuntime(10284):   at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 10-04 06:29:08.576: E/AndroidRuntime(10284):   at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-04
 06:29:08.576: E/AndroidRuntime(10284):     at
 dalvik.system.NativeStart.main(Native Method) 10-04 06:29:08.576:
 E/AndroidRuntime(10284): Caused by: java.lang.NullPointerException
 10-04 06:29:08.576: E/AndroidRuntime(10284):   at
 com.example.myExample.ProductsActivity.onCreate(ProductsActivity.java:46)
 10-04 06:29:08.576: E/AndroidRuntime(10284):   at
 android.app.Activity.performCreate(Activity.java:5133) 10-04
 06:29:08.576: E/AndroidRuntime(10284):     at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 10-04 06:29:08.576: E/AndroidRuntime(10284):   at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
 10-04 06:29:08.576: E/AndroidRuntime(10284):   ... 11 more

As you may see it gives on line: 10-04 06:29:08.576: E/AndroidRuntime(10284): at com.example.myExample.ProductsActivity.onCreate(ProductsActivity.java:46)正如您在网上看到的那样:10-04 06:29:08.576: E/AndroidRuntime(10284): at com.example.myExample.ProductsActivity.onCreate(ProductsActivity.java:46)

Which is:这是:

inputSearch.addTextChangedListener(new TextWatcher() {

If I remove the option of search, the products are displayed and I don't get any error.如果我删除搜索选项,则会显示产品并且不会出现任何错误。 Thanks.谢谢。

Logcat says it all: Logcat 说明了一切:

Caused by: java.lang.NullPointerException at com.example.myExample.ProductsActivity.onCreate(ProductsActivity.java:46)

Check this line if it is null or not.检查此行是否为空。

If that line is the one you declared on question... Then probably your "inputSearch" is null.如果该行是您在问题上声明的那一行……那么您的“inputSearch”可能为空。 Be sure that you find this view on correct layout.确保您在正确的布局上找到此视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM