gpt4 book ai didi

适配器中的 Android NullPointErexception

转载 作者:行者123 更新时间:2023-11-30 03:40:57 25 4
gpt4 key购买 nike

问题是什么?请参阅上面的代码。

Logcat 错误:

04-02 18:14:27.294: E/AndroidRuntime(1931): FATAL EXCEPTION: main
04-02 18:14:27.294: E/AndroidRuntime(1931): java.lang.NullPointerException
04-02 18:14:27.294: E/AndroidRuntime(1931): at com.insidetec.itfeed.EfficientAdapter.getView(EfficientAdapter.java:89)
04-02 18:14:27.294: E/AndroidRuntime(1931): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-02 18:14:27.294: E/AndroidRuntime(1931): at android.widget.ListView.makeAndAddView(ListView.java:1831)
04-02 18:14:27.294: E/AndroidRuntime(1931): at android.widget.ListView.fillDown(ListView.java:674)
04-02 18:14:27.294: E/AndroidRuntime(1931): at android.widget.ListView.fillFromTop(ListView.java:735)
04-02 18:14:27.294: E/AndroidRuntime(1931): at android.widget.ListView.layoutChildren(ListView.java:1652)
04-02 18:14:27.294: E/AndroidRuntime(1931): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
04-02 18:14:27.294: E/AndroidRuntime(1931): at

EfficientAdapter.java(显示帖子的适配器,提取 XML):

public class EfficientAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Post> data;
private static LayoutInflater inflater = null;
//public ImageLoader imageLoader;
ViewHolder holder;

EfficientAdapter(Activity a, ArrayList<Post> d) {

activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader = new ImageLoader(activity.getApplicationContext());

}

@Override
public int getCount() {
return data.toArray().length;

}

@Override
public Object getItem(int position) {

return position;
}

@Override
public long getItemId(int position) {

return position;
}

public static class ViewHolder {
public TextView label;
public TextView addr;
public ImageView image;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;

if (convertView == null) {
vi = inflater.inflate(R.layout.rows, null);
holder = new ViewHolder();
holder.label = (TextView) vi.findViewById(R.id.title);
holder.addr = (TextView) vi.findViewById(R.id.details);
holder.image = (ImageView) vi.findViewById(R.id.thumb);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();

holder.label.setText(data.get(position).getTitle());
holder.addr.setText(data.get(position).getPubDate());

// imageLoader.DisplayImage((data.get(position).getThumbnail()), activity,
// holder.image, 72, 72);
URL url = null;
try {
url = new URL((data.get(position).getThumbnail()));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream content = null;
try {
content = (InputStream)url.getContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(content , "src");
Bitmap mIcon1 = null;
try {
mIcon1 =
BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72, false));


return vi;
}

我找不到日志中显示的错误!我想为我的网站创建一个 RSS 提要。

最佳答案

这一行

holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72, false));

导致异常,因为您的 mIcon1null

你不能用这种方式检索你的图像

 mIcon1 =    BitmapFactory.decodeStream(url.openConnection().getInputStream());

你可能会得到 NetworkOnMainThreadException 和 NullPointerException 因为你的 mIcon 是 null

尝试使用 UniversalImageDownloader

关于适配器中的 Android NullPointErexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772623/

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