0

I am using this Custom Adapter for my ListView:

public class SideMenuAdapter extends BaseAdapter {

    private static final int TYPE_MAX_COUNT = 2;
    private static LayoutInflater inflater = null;
    private Activity activity;
    public static String[] values;
    ListView myList;

    public SideMenuAdapter(Activity a, String[] sa, ListView lv) {

        values = sa;
        activity = a;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myList = lv;
    }

    public int getCount() {

        return values.length;
    }

    public Object getItem(int position) {

        return position;
    }

    public long getItemId(int position) {

        return position;
    }

    public static class ViewHolder {

        public TextView mainText;
        public TextView sideText;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = new ViewHolder();
        View vi = convertView;
        if (vi == null) {

            //here i am getting NullPointerException
            vi.setTag(holder);
        } else {

            holder = (ViewHolder) vi.getTag();
        }
        vi = inflater.inflate(R.layout.side_menu_list_item, null);
        holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
        holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);
        holder.mainText.setText(values[position]);
        if(position == 2){

            holder.sideText.setText("3");
            holder.sideText.setBackgroundResource(R.drawable.orange);
        }
        return convertView;
    }
    @Override
    public int getViewTypeCount() {

        return TYPE_MAX_COUNT;
    }
}

i am setting adapter to my listview this way:

    String menuItems[] = new String[] { "My Wants", "Profile", "Notifications",
            "Feedback", "Logout" };
    listView1.setAdapter(new SideMenuAdapter(this, menuItems, listView1));

where am i wrong??

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

2 Answers2

3

You should initialize vi before your vi.setTag(holder);

Saasha
  • 277
  • 1
  • 2
  • 8
3

Try this, might help u...

public View getView(int position, @Nullable View convertView, ViewGroup parent) {

    ViewHolder holder = new ViewHolder();
    View vi = convertView;
    if (vi == null) {
        LayoutInflater inflater = ((Activity)activity).getLayoutInflater();
        vi = inflater.inflate(R.layout.side_menu_list_item, null);
        holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
        holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);
        vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();
    }

    holder.mainText.setText(values[position]);
    if(position == 2){

        holder.sideText.setText("3");
        holder.sideText.setBackgroundResource(R.drawable.orange);
    }
    return convertView;
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Rahmathullah M
  • 2,676
  • 2
  • 27
  • 44
  • yay... :) this help me: http://stackoverflow.com/questions/12590342/nullpointer-exception-by-findviewbyid-in-android – user568021 Dec 04 '13 at 05:21