Android LayoutFlater中的空指针异常
我创建了一个Bean类,在这个类中我们获取并设置name和image的值,并创建了另外两个类。 我制作了Android LayoutFlater中的空指针异常,android,listview,layout-inflater,Android,Listview,Layout Inflater,我创建了一个Bean类,在这个类中我们获取并设置name和image的值,并创建了另外两个类。 我制作了MainActivity类和ListAdapterPlay类,我想在其中设置listView项中的图像。 但是在LayoutInflater public class ListAdapterPlay extends BaseAdapter { // private Bean bean; private ArrayList<Bean> objarraylist; private Co
MainActivity
类和ListAdapterPlay
类,我想在其中设置listView项中的图像。
但是在LayoutInflater
public class ListAdapterPlay extends BaseAdapter {
// private Bean bean;
private ArrayList<Bean> objarraylist;
private Context context;
private LayoutInflater objlayoutinflater;
public ListAdapterPlay(ArrayList<Bean> objarraylist, Context context) {
this.context = context;
this.objarraylist = objarraylist;
objlayoutinflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //error at this line
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Bean bean = (Bean) getItem(position);
convertView = objlayoutinflater.inflate(R.layout.listviewrow,null);
// TODO Auto-generated method stub
TextView txt = (TextView) convertView.findViewById(R.id.textView1);
// bean= new Bean();
txt.setText(bean.getName());
ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);
String uri = "drawable/" + bean.getImage();
int imageResource = context.getResources().getIdentifier(uri, null,
context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
img.setImageDrawable(image);
// img.setBackgroundResource()
return convertView;
}
}
我想在listView项中设置图像,但它是通过ListAdapterPlay
类中的NullPointerException
设置的。
错误在这一行
objlayoutinflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我被困在这种情况下为什么?它给出空点异常
public ListAdapterPlay(ArrayList<Bean> objarraylist, Context context) {
this.context = context;
this.objarraylist = objarraylist;
objlayoutinflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //error at this line
// TODO Auto-generated constructor stub
}
在构造列表适配器播放时
试一试
在哪一行,ListAdapterPlay中出现空指针异常。
public ListAdapterPlay(ArrayList<Bean> objarraylist, Context context) {
this.context = context;
this.objarraylist = objarraylist;
objlayoutinflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //error at this line
// TODO Auto-generated constructor stub
}
objlistadapter= new ListAdapterPlay(objarraylist,context);
objlistadapter= new ListAdapterPlay(objarraylist,this);