我是Android开发的菜鸟,我无法实现可搜索的ListView。我开始使用这个android hive tutorial,我制作了自定义适配器Filterable并覆盖了getFilter方法,正如SO问题所解释的here& here。但是,当我运行我的应用程序时,它会因空指针异常而崩溃。任何帮助将不胜感激。
主要活动
public class StoresActivity extends SherlockActivity /*implements OnQueryTextListener*/ {
ArrayList<HashMap<String, String>> storeList;
ListView list;
LazyAdapter adapter;
EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stores);
storeList = new ArrayList<HashMap<String, String>>();
list=(ListView)findViewById(R.id.list);
SharedPreferences preferences = getSharedPreferences("Oleref", 2);
String employee = preferences.getString("employee", null);
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.bhfd.com"+ employee});
try {
storeList = task.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
adapter=new LazyAdapter(this, storeList);
list.setAdapter(adapter);
list.setItemsCanFocus(true);
//Search Bar for Listview
inputSearch = (EditText) findViewById(R.id.inputSearch); //<--search bar
if(adapter!=null){
}
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
StoresActivity.this.adapter.getFilter().filter(cs); //<--NullPointer here
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
自定义适配器
public class LazyAdapter extends BaseAdapter implements Filterable {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public String sn;
StoresActivity storesActivity;
HashMap<String, String> stores;
HashMap<String, String> storedata;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.store_row, null);
TextView store = (TextView)vi.findViewById(R.id.storeText); // title
TextView store_num = (TextView)vi.findViewById(R.id.store_num); // artist name
TextView street = (TextView)vi.findViewById(R.id.address);
TextView city = (TextView)vi.findViewById(R.id.city);
Button newbutton=(Button)vi.findViewById(R.id.newser);
Button historybutton=(Button)vi.findViewById(R.id.history);
storedata = new HashMap<String, String>();
storedata = data.get(position);
newbutton.setTag(position);
newbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent chart = new Intent();
chart.setClass(v.getContext(), SectionActivity.class);
storedata = data.get((Integer)v.getTag());
sn = storedata.get("Store_Num");
String store = storedata.get("Store");
chart.putExtra("sn", sn);
chart.putExtra("store", store);
chart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(chart);
}
});
historybutton.setTag(position);
historybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent chart = new Intent();
chart.setClass(v.getContext(), HistoryList.class);
storedata = data.get((Integer)v.getTag());
sn = storedata.get("Store_Num");
String store = storedata.get("Store");
chart.putExtra("sn", sn);
chart.putExtra("store", store);
chart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(chart);
}
});
// Setting all values in listview
store.setText(storedata.get("Store"));
store_num.setText(storedata.get("Store_Num"));
street.setText(storedata.get("Address"));
city.setText(storedata.get("City") + " " + storedata.get("State") + " " + storedata.get("Zip"));
return vi;
}
public boolean onLoadClass(Class arg0) {
// TODO Auto-generated method stub
return false;
}
//Added for search BJR 5-16-2013
public android.widget.Filter getFilter() {
return null;
}
}
logcat的
05-16 15:37:04.281: E/AndroidRuntime(26232): FATAL EXCEPTION: main
05-16 15:37:04.281: E/AndroidRuntime(26232): java.lang.NullPointerException
05-16 15:37:04.281: E/AndroidRuntime(26232): at com.tpssquared.ole.StoresActivity$1.onTextChanged(StoresActivity.java:89)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.widget.TextView.sendOnTextChanged(TextView.java:7875)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.widget.TextView.handleTextChanged(TextView.java:7922)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8285)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:725)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:201)
05-16 15:37:04.281: E/AndroidRuntime(26232): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:154)
05-16 15:37:04.281: E/AndroidRuntime(26232): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
05-16 15:37:04.281: E/AndroidRuntime(26232): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.os.Looper.loop(Looper.java:137)
05-16 15:37:04.281: E/AndroidRuntime(26232): at android.app.ActivityThread.main(ActivityThread.java:4722)
05-16 15:37:04.281: E/AndroidRuntime(26232): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 15:37:04.281: E/AndroidRuntime(26232): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 15:37:04.281: E/AndroidRuntime(26232): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
05-16 15:37:04.281: E/AndroidRuntime(26232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
05-16 15:37:04.281: E/AndroidRuntime(26232): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
//Added for search BJR 5-16-2013
public android.widget.Filter getFilter() {
return null;
}
然而你打电话给getFilter.filter()
。我想你忘了实施一些东西了