Makoto Makoto - 11 days ago 7
Android Question

Custom Adapter error in getView()

I have a

Custom
adapter that extends
BaseAdapter
, it's for a Navigation Drawer.

But for some reason once I set my listview adapter I get an error;
NullPointException
; in the
getView()
method, where I declare the
ImageView
.
Despite many researches I can't understand why. Anyone knows?
the App uses support
v7
and
ActionBarActivity


Thx!

public class DrawerItemAdapter extends BaseAdapter {

Context mContext;
int mLayoutResourceId;
ObjectDrawerItem mData[] = null;


// Constructor
public DrawerItemAdapter(Context context, int layoutResourceId, ObjectDrawerItem[] data) {
this.mContext = context;
this.mLayoutResourceId = layoutResourceId;
this.mData = data;
}


@Override
public int getCount() {
return mData.length;
}

@Override
public Object getItem(int position) {
return mData[position];
}

@Override
public long getItemId(int position) {
return 0;
}

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

View listItem = convertView;
ObjectDrawerItem objectDrawerItem = mData[position];

LayoutInflater inflater = ((ActionBarActivity) mContext).getLayoutInflater();
inflater.inflate(mLayoutResourceId, parent, false);

ImageView iconImageView = (ImageView) listItem.findViewById(R.id.drawer_item_icon);
TextView titleTextView = (TextView) listItem.findViewById(R.id.drawer_item_title);

iconImageView.setImageDrawable(listItem.getResources().getDrawable(objectDrawerItem.getmIcon()));
titleTextView.setText(objectDrawerItem.getmTitle());

return listItem;
}
}

Jun Jun
Answer

I'm looking at this line: inflater.inflate(mLayoutResourceId, parent, false);

You seem to have forgotten to assign the returned view to listItem