Picasso - java.lang.NullPointerException:key == null

时间:2013-07-30 14:47:26

标签: android android-asynctask image-caching picasso

我在我的一个应用程序中使用Picasso来下载大量图像以供将来使用,因为应用程序在登录后将完全脱机模式。

因此,在成功登录后,我尝试使用Progress

在AsyncTask中下载图像

为了实现这个目标,我编写了以下代码来下载一堆图像

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

import com.squareup.picasso.Picasso;

public class CacheActivity extends Activity {
    int size = 0;
    final List<String> urls = new ArrayList<String>();
    int totalImageSize;
    static Picasso picasso;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cache);

        Collections.addAll(urls, Data.URLS);
        Collections.addAll(urls, Data.URLS);
        Collections.addAll(urls, Data.URLS);
        totalImageSize = urls.size();

        picasso = Picasso.with(CacheActivity.this);
        picasso.setDebugging(true);

        new TestTask().execute();

    }

    public class TestTask extends AsyncTask<Void, Integer, Void>

    {
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(CacheActivity.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(totalImageSize);
            dialog.setMessage("1 - 100");
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            for (Iterator iterator = urls.iterator(); iterator.hasNext();) {
                final String url = (String) iterator.next();

                try {
                    picasso.load(url).get();
                    } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                size = size + 1;
                publishProgress(size);

            System.out.println("CacheActivity.TestTask.doInBackground()");

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            dialog.dismiss();
        }

    }

}

但是当我运行应用程序时,我收到以下错误

07-30 12:55:42.334: W/System.err(2369): java.lang.NullPointerException: key == null
07-30 12:55:42.334: W/System.err(2369):     at com.squareup.picasso.LruCache.get(LruCache.java:50)
07-30 12:55:42.334: W/System.err(2369):     at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:101)
07-30 12:55:42.334: W/System.err(2369):     at com.squareup.picasso.RequestBuilder.get(RequestBuilder.java:305)
07-30 12:55:42.344: W/System.err(2369):     at com.example.picasso.CacheActivity$TestTask.doInBackground(CacheActivity.java:61)
07-30 12:55:42.344: W/System.err(2369):     at com.example.picasso.CacheActivity$TestTask.doInBackground(CacheActivity.java:1)
07-30 12:55:42.344: W/System.err(2369):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-30 12:55:42.344: W/System.err(2369):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-30 12:55:42.344: W/System.err(2369):     at java.lang.Thread.run(Thread.java:856)
07-30 12:55:42.334: W/System.err(2369): java.lang.NullPointerException: key == null
07-30 12:55:42.334: W/System.err(2369):     at com.squareup.picasso.LruCache.get(LruCache.java:50)
07-30 12:55:42.334: W/System.err(2369):     at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:101)
07-30 12:55:42.334: W/System.err(2369):     at com.squareup.picasso.RequestBuilder.get(RequestBuilder.java:305)
07-30 12:55:42.344: W/System.err(2369):     at com.example.picasso.CacheActivity$TestTask.doInBackground(CacheActivity.java:61)
07-30 12:55:42.344: W/System.err(2369):     at com.example.picasso.CacheActivity$TestTask.doInBackground(CacheActivity.java:1)
07-30 12:55:42.344: W/System.err(2369):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-30 12:55:42.344: W/System.err(2369):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-30 12:55:42.344: W/System.err(2369):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-30 12:55:42.344: W/System.err(2369):     at java.lang.Thread.run(Thread.java:856)

我有什么问题吗?

1 个答案:

答案 0 :(得分:0)

嗯,这是一个有趣的问题。看起来你也在使用Picasso 2.0。你确认你没有通过null吗?

我需要一个示例应用程序(包括URL)来调试它。你可以打包并附上吗?

编辑:现在已在Picasso 2.0中修复。

https://github.com/square/picasso