LayoutInflater中的空指针异常

时间:2013-04-02 11:07:24

标签: android listview layout-inflater

我已经制作了Bean课程,我们得到了&设置名称和值的值图像并使另外两个类。    我创建了MainActivity类和ListAdapterPlay类,我想在listView项中设置图像。     但它会在NullPointerException

中提供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;
}
}

我将MainActivity课程改为如下

public class MainActivity extends Activity {
private ArrayList<Bean> objarraylist;
private  ListAdapterPlay objlistadapter;
private ListView listview;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    objarraylist=new ArrayList<Bean>();
    objarraylist.add(new Bean("RAM","ic_launcher"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));
    objarraylist.add(new Bean("RAM","lion"));

    objlistadapter= new ListAdapterPlay(objarraylist,context);
    listview=(ListView)this.findViewById(R.id.listview1);
    listview.setAdapter(objlistadapter);
}

}

我想在listView项目中设置图片,但是要通过NullPointerException类中的ListAdapterPlay设置图像。   错误就在这一行

objlayoutinflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我遇到这种情况为什么?它给NullPointerException

1 个答案:

答案 0 :(得分:2)

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);

在构建ListAdapterPlay

objlistadapter= new ListAdapterPlay(objarraylist,this);