适配器中的Android Null Pointer异常

时间:2013-04-02 19:25:39

标签: android nullpointerexception

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

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源。

1 个答案:

答案 0 :(得分:1)

这一行

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

导致异常,因为您的mIcon1null

你无法以这种方式检索你的图像

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

您可能会收到NetworkOnMainThreadException和NullPointerException,因为您的mIcon为空

尝试使用UniversalImageDownloader