简体   繁体   English

LazyAdapter上的NullPointerException

[英]NullPointerException at LazyAdapter

I am trying to perform a search on LazyAdapter class by creating a custom Filter. 我试图通过创建自定义过滤器对LazyAdapter类执行搜索。 But when I am trying to perform the search by using the TextWatcher, the application is forcing close. 但是,当我尝试使用TextWatcher执行搜索时,该应用程序将强制关闭。 The code for the LazyAdapter class is as follows: package com.demo.directory; LazyAdapter类的代码如下:package com.demo.directory; public class LazyAdapter extends BaseAdapter { 公共类LazyAdapter扩展BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public View getFilter(CharSequence seq, View convertView) {

    String name_org;
    View vi = convertView;
    convertView = null;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView name = (TextView) vi.findViewById(R.id.name_org); // title
    TextView address = (TextView) vi.findViewById(R.id.address_org); // artist
                                                                        // name
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
                                                                            // image

    for (int i = 0; i < data.size(); i++) {
        HashMap<String, String> org = new HashMap<String, String>();
        org = data.get(i);
        name_org = org.get(OrganizationActivity.KEY_NAME);

        if (name_org != null && seq != null) {
            if (name_org.contains(seq)) {
                name.setText(org.get(OrganizationActivity.KEY_NAME));
                address.setText(org.get(OrganizationActivity.KEY_CITY)
                        + ", " + org.get(OrganizationActivity.KEY_STATE));
                imageLoader.DisplayImage(
                        org.get(OrganizationActivity.KEY_IMAGE_URL),
                        thumb_image);
                notifyDataSetChanged();
            } else {
                org.remove(OrganizationActivity.KEY_NAME);
            }
        }
    }
    return vi;
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView name = (TextView) vi.findViewById(R.id.name_org); // title
    TextView address = (TextView) vi.findViewById(R.id.address_org); // artist
                                                                        // name
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
                                                                            // image

    HashMap<String, String> org = new HashMap<String, String>();
    org = data.get(position);

    // Setting all values in listview
    name.setText(org.get(OrganizationActivity.KEY_NAME));
    address.setText(org.get(OrganizationActivity.KEY_CITY) + ", "
            + org.get(OrganizationActivity.KEY_STATE));
    imageLoader.DisplayImage(org.get(OrganizationActivity.KEY_IMAGE_URL),
            thumb_image);
    return vi;
}
}

The above code is now working, but it doesn't show anything if I delete the search data, ie, the original data of the ListView is not returned if the EditText data is cleared back after searching.. 上面的代码现在可以正常工作,但是如果我删除搜索数据,它不会显示任何内容,即,如果在搜索后清除了EditText数据,则不会返回ListView的原始数据。

Are you sure your name_org and s variables are not null ??? 您确定您的name_orgs变量不为 name_org

You have to first check it, like, 您必须先检查它,例如

if(name_org != null && s != null)
{
 if(name_org.contains(s))
 {
 // Your other stuff goes here...
 .
 .
 }
}

To retain data items after you clear search edittext, below code is useful. 要在清除搜索编辑文本后保留数据项,以下代码很有用。 Hope this helps you. 希望这对您有所帮助。

public Filter getFilter() 
{
    Filter filter = new Filter() 
    {           
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) 
        {
            System.out.println(" --- constraint in publishResults --- "+constraint+" - results - "+results);
            itemDetailList = (List<Dish>)results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) 
        {
            FilterResults filterResults = new FilterResults();
            List<Dish> filtered_list = new ArrayList<Dish>();
            if(originalItemsDetails == null)
            {
                System.out.println(" --- originalItemsDetails IS NULL --- ");
                originalItemsDetails = new ArrayList<Dish>(itemDetailList);
            }
            else
            {
                System.out.println(" --- originalItemsDetails NOT NULL --- ");
            }

            if(constraint == null || constraint.length() == 0) 
            {
                filterResults.count = originalItemsDetails.size();
                filterResults.values = originalItemsDetails;
            } 
            else 
            {
                constraint = constraint.toString().toLowerCase();
                for(int i=0; i<originalItemsDetails.size(); i++) 
                {
                    String dataNames = originalItemsDetails.get(i).getDishName().toLowerCase();
                    if(dataNames.startsWith(constraint.toString()))  
                    {
                        filtered_list.add(originalItemsDetails.get(i));
                    }
                }

                filterResults.count = filtered_list.size();
                System.out.println(" --- filterResults.count --- "+filterResults.count);

                filterResults.values = filtered_list;
                System.out.println(" --- filterResults.values --- "+filterResults.values);
            }

            System.out.println(" --- constraint in performFiltering --- "+constraint);

            return filterResults;
        }
    };

    return filter;
}

You can try and create a custom Filter that can help in showing the data back again once the data in the edit text becomes empty. 您可以尝试创建一个自定义过滤器,一旦编辑文本中的数据为空,该过滤器将有助于再次显示数据。 Or you can create two adapters for the listview, first one containing the original data of the listview and the second one which displays the search results for the edit text search. 或者,您可以为列表视图创建两个适配器,第一个适配器包含列表视图的原始数据,第二个适配器显示用于编辑文本搜索的搜索结果。 Once the data in edit text becomes empty, set the List Adapter to the initial one. 一旦编辑文本中的数据为空,将列表适配器设置为初始适配器。

I think s is null, have you checked it. 我认为s为null,您检查了吗。 Because contains method throws NPE even if charsequence that you pass is null 因为包含的方法即使您传递的字符序列为null也会抛出NPE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM