我想简单地通过 EditText 过滤我的 listViews 的项目,我使用实现 Filterable 的 ArrayAdapter 来执行此操作,但由于某种原因,我得到了一个巨大的 NullPointerException ......我通过一些教程做到了这一点,我只是无法弄清楚我试过的问题是什么尝试捕获我的代码中的所有内容,但我无法使其正常工作。
当我尝试在我的 EditText 中输入文本时,它被冻结然后我崩溃了:
任何建议将不胜感激。谢谢。
07-06 11:35:15.785: E/AndroidRuntime(28779): FATAL EXCEPTION: main
07-06 11:35:15.785: E/AndroidRuntime(28779): java.lang.NullPointerException
07-06 11:35:15.785: E/AndroidRuntime(28779): at com.KeySoft.OpenGuide.Adapter.SearchAdapter.getView(SearchAdapter.java:61)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.AbsListView.obtainView(AbsListView.java:1533)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.ListView.makeAndAddView(ListView.java:1793)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.ListView.fillSpecific(ListView.java:1338)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.ListView.layoutChildren(ListView.java:1624)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.AbsListView.onLayout(AbsListView.java:1384)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.view.View.layout(View.java:7228)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.view.View.layout(View.java:7228)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.view.View.layout(View.java:7228)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.view.View.layout(View.java:7228)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.view.View.layout(View.java:7228)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.os.Looper.loop(Looper.java:123)
07-06 11:35:15.785: E/AndroidRuntime(28779): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-06 11:35:15.785: E/AndroidRuntime(28779): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 11:35:15.785: E/AndroidRuntime(28779): at java.lang.reflect.Method.invoke(Method.java:507)
07-06 11:35:15.785: E/AndroidRuntime(28779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-06 11:35:15.785: E/AndroidRuntime(28779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-06 11:35:15.785: E/AndroidRuntime(28779): at dalvik.system.NativeStart.main(Native Method)
这是我的带过滤器的适配器:
public class SearchAdapter extends ArrayAdapter<KeresesBejegyzes> implements Filterable
{
private Activity context;
private List<KeresesBejegyzes> bejegyzesek;
private List<KeresesBejegyzes> eredetiBejegyzesek;
static class ViewHolder
{
public TextView shopNameTv;
public TextView shopTavTv;
}
public SearchAdapter(Activity context, List<KeresesBejegyzes> bejegyzesek_)
{
super(context, R.layout.rowlayout, bejegyzesek_);
this.context = context;
this.bejegyzesek = bejegyzesek_;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.shopNameTv = (TextView) rowView.findViewById(R.id.shopNameTv);
viewHolder.shopTavTv = (TextView) rowView.findViewById(R.id.tavTv);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
KeresesBejegyzes bejegyzes = bejegyzesek.get(position);
holder.shopNameTv.setText(bejegyzes.getNev());
if (bejegyzes.getTavolsag() != 0)
{
holder.shopTavTv.setText(Double.toString(bejegyzes.getTavolsag()) + " km - " + bejegyzes.getTipus());
}
else
{
holder.shopTavTv.setText(bejegyzes.getTipus());
}
return rowView;
}
@Override
public Filter getFilter()
{
try
{
Filter filter = new Filter()
{
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
bejegyzesek = (List<KeresesBejegyzes>) results.values; // has
// the
// filtered
// values
notifyDataSetChanged(); // notifies the data with new
// filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults(); // Holds the
// results
// of a
// filtering
// operation
// in values
List<KeresesBejegyzes> FilteredArrList = new ArrayList<KeresesBejegyzes>();
if (eredetiBejegyzesek == null)
{
eredetiBejegyzesek = new ArrayList<KeresesBejegyzes>(bejegyzesek); // saves
// the
// original
// data
// in
// mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null
* returns the mOriginalValues(Original) values else does
* the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0)
{
// set the Original result to return
results.count = eredetiBejegyzesek.size();
results.values = eredetiBejegyzesek;
}
else
{
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < eredetiBejegyzesek.size(); i++)
{
KeresesBejegyzes data = eredetiBejegyzesek.get(i);
if (data.getNev().toLowerCase().contains(constraint.toString()) || data.getCim().toLowerCase().contains(constraint.toString()))
{
FilteredArrList.add(data);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
catch (Exception e)
{
Log.e("SearchAdapterError", e.toString());
}
return null;
}
}
这是EditText:
searchEditText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
try
{
if(searchAdapter.getFilter() != null)
{
searchAdapter.getFilter().filter(s.toString());
}
else
{
ki("Error 8: searchAdapter.getFilter() == null");
}
}
catch(Exception e)
{
ki("Error 7:"+e);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
编辑 dj aqeel:
当我尝试通过 EditText 搜索时,我只得到空指针 ex,我的 listView 很好地显示了我的列表,我什至可以对其进行排序,效果很好,没有任何例外。
初始化:(我刚刚意识到 bejegyzesek 不是一个列表,它是一个向量,这可能是一个问题吗?这是一个旧代码改造,所以我错误地保持不变..)
所以这是在我的活动中:
public class Search extends Activity
{
Vector<KeresesBejegyzes> bejegyzesek = new Vector<KeresesBejegyzes>();
// (...more code here...)
db.open();
Cursor c = db.getAllShopsFullTitles();
if(c.moveToFirst())
{
do
{
KeresesBejegyzes kb = new KeresesBejegyzes();
kb.setNev(c.getString(1));
kb.setTipus(c.getString(2));
kb.setLatitude(Double.parseDouble(c.getString(4)));
kb.setLonditude(Double.parseDouble(c.getString(5)));
bejegyzesek.add(kb);
}while(c.moveToNext());
}
db.close();
listView = (ListView)findViewById(R.id.listView);
searchAdapter = new SearchAdapter(this, bejegyzesek);
listView.setAdapter(searchAdapter);