【发布时间】:2015-04-06 16:11:55
【问题描述】:
我一定遗漏了一些明显但无法找到的东西。
在下面的函数中,如果我尝试加载我的 Drawable 数组,我得到一个空指针异常,说 getResource 在空指针处被调用,而如果我直接传递 drawable 并在上下文的帮助下膨胀它,它工作正常。
我在这里做错了什么?请帮忙。
class MyDrawerAdapter extends BaseAdapter {
private Context context;
String[] drawerList;
public MyDrawerAdapter(Context context) {
drawerList = context.getResources().getStringArray(R.array.Cat_array);
this.context = context;
}
/*
Drawable[] drawableArr ={MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home),
MrVector.inflate(context.getResources(),R.drawable.home)};
*/
@Override
public int getCount() {
return drawerList.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
if (position == 3 || position == 11) {
return 1;
}
if (position == 4) {
return 2;
}
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
if (getItemViewType(position) == 1) {
if (convertView == null) {
row = inflater.inflate(R.layout.divider, parent, false);
} else {
row = convertView;
}
row.setEnabled(false);
row.setOnClickListener(null);
}
if (getItemViewType(position) == 0) {
if (convertView == null) {
row = inflater.inflate(R.layout.drawer_list_item, parent, false);
} else {
row = convertView;
}
TextView titletext = (TextView) row.findViewById(R.id.drawer_text);
ImageView titleImage = (ImageView) row.findViewById(R.id.drawer_image);
titletext.setText(drawerList[position]);
//titleImage.setImageDrawable(drawableArr[position]);
titleImage.setImageDrawable(MrVector.inflate(context.getResources(),R.drawable.home));
}
if (getItemViewType(position) == 2) {
if (convertView == null) {
row = inflater.inflate(R.layout.header_listview_menu, parent, false);
} else {
row = convertView;
}
row.setEnabled(false);
row.setOnClickListener(null);
}
return row;
}
}
【问题讨论】:
-
问题一定出在调用代码上。
-
如何调用MyDrawerAdapter的构造函数?
-
@DavidJhons mMyDrawerAdapter = new MyDrawerAdapter(this);在我的 MainActivity 中。
-
也请贴日志猫
-
你能显示调用代码吗?类似问题stackoverflow.com/questions/29490118/…
标签: android baseadapter android-context