NetworkImageView崩溃

时间:2014-03-19 17:21:08

标签: android listview networkimageview

我的应用有ListView的活动,对于列表中的每个单元格,我需要从网址加载图片。但是,当我添加行以加载图像时,应用程序崩溃。

我不知道错误是由我的代码还是列表视图的某些单元重用行为引起的,因为Logcat没有指向我的代码中的任何行。 我确保传递的网址是正确的,并且可能不会干扰。

我跟着this tutorial编写了这段代码。

这是ListView适配器类,我在getView(...)加载图片:

class CellAdapter extends BaseAdapter {

    Context context;
    List<Product> productList;
    private static LayoutInflater inflater = null;

    public CellAdapter(Context context, List<Product> productList) {
        this.context = context;
        this.productList = productList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return productList.size();
    }

    @Override
    public Product getItem(int position) {
        return productList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        while (view == null)
            view = inflater.inflate(R.layout.product_result, null);

        NetworkImageView image = (NetworkImageView) view.findViewById(R.id.cell_product_image);
        image.setDefaultImageResId(R.drawable.image_placeholder);
        image.setImageUrl(getItem(position).imageURL, APIManager.getSharedImageLoader());

        return view;
    }
}

上面使用的ImageLoader设置如下:

sharedImageLoader = new ImageLoader(Volley.newRequestQueue(context), new BitmapLruCache());

BitmapLruCache是:

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {

    public BitmapLruCache() {
        this(getDefaultLruCacheSize());
    }

    public BitmapLruCache(int sizeInKiloBytes) {
        super(sizeInKiloBytes);
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight() / 1024;
    }

    @Override
    public Bitmap getBitmap(String url) {
        return get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }

    public static int getDefaultLruCacheSize() {
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        return maxMemory / 8;
    }
}

这是Logcat错误:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.android.volley.Request.<init>(Request.java:137)
            at com.android.volley.toolbox.ImageRequest.<init>(ImageRequest.java:71)
            at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:219)
            at com.android.volley.toolbox.NetworkImageView.loadImageIfNecessary(NetworkImageView.java:149)
            at com.android.volley.toolbox.NetworkImageView.onLayout(NetworkImageView.java:198)
            at android.view.View.layout(View.java:7175)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
            at android.view.View.layout(View.java:7175)
            at android.widget.ListView.setupChild(ListView.java:1833)
            at android.widget.ListView.makeAndAddView(ListView.java:1748)
            at android.widget.ListView.fillDown(ListView.java:670)
            at android.widget.ListView.fillFromTop(ListView.java:727)
            at android.widget.ListView.layoutChildren(ListView.java:1598)
            at android.widget.AbsListView.onLayout(AbsListView.java:1262)
            at android.view.View.layout(View.java:7175)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
            at android.view.View.layout(View.java:7175)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:343)
            at android.view.View.layout(View.java:7175)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
            at android.view.View.layout(View.java:7175)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:343)
            at android.view.View.layout(View.java:7175)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:343)
            at android.view.View.layout(View.java:7175)
            at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3714)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)

这是包含空指针异常的方法:

    public Request(int method, String url, Response.ErrorListener listener) {
        mMethod = method;
        mUrl = url;
        mErrorListener = listener;
        setRetryPolicy(new DefaultRetryPolicy());
        /* the next line is line 137 with the Exception */
        mDefaultTrafficStatsTag = TextUtils.isEmpty(url) ? 0: Uri.parse(url).getHost().hashCode(); 
    }

1 个答案:

答案 0 :(得分:3)

您正在使用此库的过时版本,该版本缺少以下两个最近的提交:

  

6c33917 Merge&#34;防范当前正在发生的NullPointerException   在Volley中,当一个请求被给出一个主机为空的URL时。&#34;由Ficus   Kirkpatrick - 3周前idea133-weekly-release master

     

0ec9297防范当前正在发生的NullPointerException   当请求被给予一个主机为空的URL时,排队。由康 - 3   几周前

使用git更新您的工作版本以反映项目存储库的当前状态,您应该没问题。检查这些差异,更改基本上是添加和处理空检查。