0

我正在尝试在后台下载图像,以便在下载图像时应用程序不会挂起。我得到了一个nullpointerexception,我不明白我做错了什么。我的日志说我正在按预期从 URL 中获取数据,所以我不确定为什么这不起作用。谢谢!

这是我拥有的代码:

public class DownloadImageBackground extends AsyncTask<String, Void, Drawable> {

ImageView imageView = null;
String myURL = null;

@Override
protected Drawable doInBackground(String...strings) {
    this.myURL = strings[0];
    Log.i("doInBackground", "Loading image from: "+myURL.toString());
    //TODO: Pass the correct URL to download_image
    return download_Image(myURL);
}

@Override
protected void onPostExecute(Drawable result) {
    imageView.setImageDrawable(result);
}

private Drawable download_Image(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, url);
        return d;
    } catch (Exception e) {
        Log.e("download_Image", "Caught exception: "+e.getMessage());
        return null;
    }
}
}

这是错误:

02-27 20:07:37.815: I/doInBackground(19374): Loading image from: http://www.martingrumet.com/boxelder01may04.jpg
02-27 20:07:37.815: I/Choreographer(19374): Skipped 92 frames!  The application may be doing too much work on its main thread.
02-27 20:07:37.840: D/dalvikvm(19374): GC_CONCURRENT freed 404K, 9% free 13752K/15047K, paused 2ms+12ms, total 24ms
02-27 20:07:38.110: D/dalvikvm(19374): GC_FOR_ALLOC freed 72K, 9% free 13723K/15047K, paused 11ms, total 11ms
02-27 20:07:38.110: I/dalvikvm-heap(19374): Grow heap (frag case) to 15.182MB for 1334016-byte allocation
02-27 20:07:38.125: D/dalvikvm(19374): GC_CONCURRENT freed 0K, 9% free 15026K/16391K, paused 2ms+1ms, total 12ms
02-27 20:07:38.125: D/dalvikvm(19374): WAIT_FOR_CONCURRENT_GC blocked 11ms
02-27 20:07:38.395: D/AndroidRuntime(19374): Shutting down VM
02-27 20:07:38.395: W/dalvikvm(19374): threadid=1: thread exiting with uncaught exception (group=0x40cf14a0)
02-27 20:07:38.395: E/AndroidRuntime(19374): FATAL EXCEPTION: main
02-27 20:07:38.395: E/AndroidRuntime(19374): java.lang.NullPointerException
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.ourbenefactors.treeidentification2.DownloadImageBackground.onPostExecute(DownloadImageBackground.java:27)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.ourbenefactors.treeidentification2.DownloadImageBackground.onPostExecute(DownloadImageBackground.java:1)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.os.Looper.loop(Looper.java:137)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at java.lang.reflect.Method.invoke(Method.java:511)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
02-27 20:07:38.395: E/AndroidRuntime(19374):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

4

您是否在某处初始化了 ImageView?在我看来,整个过程都是空的。

我还建议使用返回 Bitmap 而不是 Drawable的BitmapFactory.decodeStream() 。您还可以传递一个BitmapFactory.Options,它允许用于更改输入样本大小和其他几个有用的选项。

于 2013-02-28T04:26:13.490 回答
1

您的 ImageView 在imageView.setImageDrawable(result);

于 2013-02-28T04:42:42.243 回答