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.):
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)
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:
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. Be sure that you find this view on correct layout.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.