我必须遗漏一些明显但无法找到它的东西。
在下面的函数中,如果我尝试加载我的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;
}
}
答案 0 :(得分:0)
这是我已经实施的解决方法,但如果有人可以提供帮助,我们仍然希望得到答案。
class MyDrawerAdapter extends BaseAdapter {
private Context context;
String[] drawerList;
public MyDrawerAdapter(Context context) {
drawerList = context.getResources().getStringArray(R.array.Cat_array);
this.context = context;
}
@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;
int layoutID = context.getResources().getIdentifier("icon_" + position, "drawable", context.getApplicationInfo().packageName);
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(MrVector.inflate(context.getResources(),layoutID));
}
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;
}
}