下面的代码运行良好。老实说,我什至不认为我改变了什么。它停止工作。我有一个自定义列表视图,错误是在该自定义列表视图中搜索。我创建了一个自定义适配器等。
// 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
}
});
这是自定义适配器上的过滤器(我没有使用 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();
}
}
日志猫
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
正如您在网上看到的那样:10-04 06:29:08.576: E/AndroidRuntime(10284): at com.example.myExample.ProductsActivity.onCreate(ProductsActivity.java:46)
这是:
inputSearch.addTextChangedListener(new TextWatcher() {
如果我删除搜索选项,则会显示产品并且我不会收到任何错误。谢谢。