我有一个Custom
适配器扩展BaseAdapter
,它适用于导航抽屉。
但由于某种原因,一旦我设置了listview适配器,我就会收到错误; NullPointException
;在getView()
方法中,我声明了ImageView
。
尽管有许多研究我不明白为什么。有谁知道?
该应用使用支持v7
和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;
}
}
答案 0 :(得分:2)
我正在看这条线:
inflater.inflate(mLayoutResourceId, parent, false);
您似乎忘记将返回的视图分配给listItem
答案 1 :(得分:0)
使用inflater.findViewById()...
ImageView iconImageView = (ImageView) inflater.findViewById(R.id.drawer_item_icon);
TextView titleTextView = (TextView) inflater.findViewById(R.id.drawer_item_title);
用这个改变你的代码。