gpt4 book ai didi

android - Listview.setadapter 引发空指针异常

转载 作者:太空狗 更新时间:2023-10-29 15:57:31 24 4
gpt4 key购买 nike

代码如下

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

最佳答案

super.onCreate(savedInstanceState); 之后使用 setContentView(R.layout.company_list);

关于android - Listview.setadapter 引发空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5244797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com