gpt4 book ai didi

android - 通用图像加载器不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:27 25 4
gpt4 key购买 nike

我是一名初级程序员。我正在尝试使用通用图像加载器来显示从站点解析的图像,但它不起作用。我做错了什么?

谢谢。

这是一个测试程序。

package com.example.imageloader;

public class MainActivity extends ListActivity {

private final String URL = "Site_URL";
private ArrayList<Order> listItems = null;
private myAdapter myAdapter;
private ProgressDialog pd;
ImageLoader imageLoader;
DisplayImageOptions options;
String imgSrc;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItems = new ArrayList<Order>();
this.myAdapter = new myAdapter(this, R.layout.list_item, listItems);
setListAdapter(this.myAdapter);

new Parse().execute(URL);

lv = getListView();

imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));

options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher)
.showImageForEmptyUri(R.drawable.ic_launcher)
.cacheOnDisc()
.cacheInMemory()
.build();
}


private class Parse extends AsyncTask<String, Integer, ArrayList<Order>> {
@Override
protected void onPreExecute() {
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Loading");
pd.show();
}

@Override
protected ArrayList<Order> doInBackground(String... urls) {
listItems.clear();
try {
Document doc = Jsoup.connect(URL).get();
Elements img = doc.select("img");
for (Element src : img) {
String imgSrc = src.attr("src");

System.out.println(imgSrc);

Order o = new Order();
o.setLink(imgSrc);
o.setTextName(imgSrc);
listItems.add(o);
}

} catch (IOException e) {
e.printStackTrace();
}
return listItems;
}

@Override
protected void onPostExecute(ArrayList<Order> listItems) {
myAdapter = new myAdapter(MainActivity.this, R.layout.list_item, listItems);
setListAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
pd.dismiss();
}
}
}

自定义适配器我在这里遇到错误

package com.example.imageloader;

public class myAdapter extends ArrayAdapter<Order> {

private LayoutInflater lInflater;
private ArrayList<Order> data;
DisplayImageOptions options;
ImageLoader imageLoader;
private Order o;
String imgSrc;

public myAdapter(Context c, int listItem, ArrayList<Order> data) {
super(c, listItem, data);
this.data = data;
lInflater = LayoutInflater.from(c);
}

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

if (convertView == null) {
convertView = lInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.image);
holder.textName = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);

holder.textName.setOnClickListener(new OnItemClickListener(position));

} else {
holder = (ViewHolder) convertView.getTag();
}

o = data.get(position);
holder.textName.setText(o.getTextName());
holder.img.setTag(o);

try {
imageLoader.displayImage(o.getLink(), holder.img, options); // NullPointerException here!
} catch (NullPointerException e) {
e.printStackTrace();
}
} else {
return null;
}
return convertView;


static class ViewHolder {
ImageView img;
TextView textName;
}
}

日志

01-16 11:55:04.480: W/System.err(19105): java.lang.NullPointerException
01-16 11:55:04.496: W/System.err(19105): at com.example.imageloader.myAdapter.getView(myAdapter.java:62)

最佳答案

你应该添加这段代码

imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(c));

在 myAdapter 的构造函数中。并检查 imageview 的 id,它是否正确或在您引用它的正确位置

holder.img = (ImageView) convertView.findViewById(R.id.image);

关于android - 通用图像加载器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14352202/

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