适配器过滤时出现空指针异常

时间:2012-07-06 09:46:01

标签: android nullpointerexception android-adapter

我想简单地通过EditText过滤我的listViews的项目,我使用ArrayAdapter实现Filterable但是由于某种原因我得到了一个大量的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) {
                 }
            }); 

E D I T for 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);

0 个答案:

没有答案