Listview.setadapter引发nullpointer异常

时间:2011-03-09 11:01:49

标签: android

代码如下

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i = getIntent();

        ArrayList<String> imageUrl = i.getStringArrayListExtra("ImageUrl");
        String[] companyImageUrl = imageUrl.toArray(new String[0]);
            ListView companyList = null;
            LazyAdapter companyListViewAdapter;

            companyList = (ListView)findViewById(R.id.companylist);

            companyListViewAdapter = new LazyAdapter(companyListView.this , companyImageUrl);

            try {
                companyList.setAdapter(companyListViewAdapter);//exception here
            } catch (Exception e) {
                System.out.println(e);
            }
            setContentView(R.layout.company_list);
}

编辑:添加了Lazyadapter代码

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader= new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView text;
    public ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    UrlList urlList;

    urlList = XMLParser.urlList;

    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);;
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    String[] categoryNameStrings = urlList.getCategoryName().toArray(new String[0]);

    holder.text.setText(categoryNameStrings[position]);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;
}

}

4 个答案:

答案 0 :(得分:5)

setContentView(R.layout.company_list);

之后使用super.onCreate(savedInstanceState);

答案 1 :(得分:2)

将此行放在super.onCreate(savedInstanceState);

之后
setContentView(R.layout.company_list);

答案 2 :(得分:1)

首次收到错误时,首先需要确保将正确的xml文件作为

setContentView(R.layout.main)

listview id (i.e) findViewById(R.id.listview1);

答案 3 :(得分:0)

这是什么?

 UrlList urlList;
 urlList = XMLParser.urlList;

它在这做什么?

String[] categoryNameStrings = urlList.getCategoryName().toArray(new String[0]);

您能否发布错误日志?