我在我的列表视图中添加了搜索事件。但它返回java.lang.NullPointerException。
我以This为例。
这是我的活动类代码。
public void ListDrwaer() {
listData = new ArrayList<String>();
try {
JSONObject jsonResponse = new JSONObject(strJson1);
JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
restName = jsonChildNode.optString("name");
String type = jsonChildNode.optString("type");
if(type.equals("restaurant")){
listData.add(restName);
}
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
Toast.LENGTH_LONG).show();
}
if(restName!=null){
adapter = new FevListAdapter(Restaurant_List.this,listData);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
searchTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Restaurant_List.this.adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView textView = (TextView) arg1.findViewById(R.id.fav_list);
String text = textView.getText().toString();
Intent newActivity = new Intent(getBaseContext(),Fav_Tabs.class);
newActivity.putExtra("Rest_name", text);
startActivity(newActivity);
}
});
}
else{
text.setText("No data found...");
}
}
这是我的适配器类。
public class FevListAdapter extends BaseAdapter implements Filterable{
private Context context;
List<String> listData;
private List<String> list;
public FevListAdapter(Context convertView, List<String> listData){
this.context = convertView;
this.listData = listData;
//Edited
//this.list.addAll(listData);
}
.............................
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final String text = listData.get(position);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.fev_rest_list, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.fav_list);
lblListHeader.setText(text);
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
list = (List<String>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<String> FilteredArrayNames = new ArrayList<String>();
// perform your search here using the searchConstraint String.
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < list.size(); i++) {
String dataNames = list.get(i);
if (dataNames.toLowerCase().startsWith(constraint.toString())) {
FilteredArrayNames.add(dataNames);
}
}
results.count = FilteredArrayNames.size();
results.values = FilteredArrayNames;
Log.e("VALUES", results.values.toString());
return results;
}
};
return filter;
}
}
有人可以帮我解决这个问题吗?
被修改
这是我的logcat错误
02-10 22:11:45.661: E/AndroidRuntime(28229): FATAL EXCEPTION: main
02-10 22:11:45.661: E/AndroidRuntime(28229): java.lang.NullPointerException
02-10 22:11:45.661: E/AndroidRuntime(28229): at com.example.rp.FevListAdapter.<init>(FevListAdapter.java:28)
02-10 22:11:45.661: E/AndroidRuntime(28229): at com.example.rp.Restaurant_List.ListDrwaer(Restaurant_List.java:224)
02-10 22:11:45.661: E/AndroidRuntime(28229): at com.example.rp.Restaurant_List$JsonReadTask.onPostExecute(Restaurant_List.java:175)
02-10 22:11:45.661: E/AndroidRuntime(28229): at com.example.rp.Restaurant_List$JsonReadTask.onPostExecute(Restaurant_List.java:1)
02-10 22:11:45.661: E/AndroidRuntime(28229): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-10 22:11:45.661: E/AndroidRuntime(28229): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-10 22:11:45.661: E/AndroidRuntime(28229): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-10 22:11:45.661: E/AndroidRuntime(28229): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 22:11:45.661: E/AndroidRuntime(28229): at android.os.Looper.loop(Looper.java:137)
02-10 22:11:45.661: E/AndroidRuntime(28229): at android.app.ActivityThread.main(ActivityThread.java:4960)
02-10 22:11:45.661: E/AndroidRuntime(28229): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 22:11:45.661: E/AndroidRuntime(28229): at java.lang.reflect.Method.invoke(Method.java:511)
02-10 22:11:45.661: E/AndroidRuntime(28229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-10 22:11:45.661: E/AndroidRuntime(28229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-10 22:11:45.661: E/AndroidRuntime(28229): at dalvik.system.NativeStart.main(Native Method)