我正在尝试使用 AsyncTask 下载 facebook 图像配置文件,但是我知道下载的位图为空,因此我得到一个 nullPointerExceprion。
这是我的代码:
private synchronized void downloadAvatar(final String user_id,final ImageView iv) {
AsyncTask<Void, Void, Bitmap> task = new AsyncTask<Void, Void, Bitmap>() {
@Override
public Bitmap doInBackground(Void... params) {
URL fbAvatarUrl = null;
Bitmap fbAvatarBitmap = null;
try {
fbAvatarUrl = new URL("http://graph.facebook.com/"+user_id+"/picture");
fbAvatarBitmap = BitmapFactory.decodeStream(fbAvatarUrl.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fbAvatarBitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
iv.setImageBitmap(result);
}
};
task.execute();
}
(user_id) 是一个存在的配置文件 id -我在调试模式下检查了它..
我在这一行得到一个 nullPointerException:
iv.setImageBitmap(result);
(结果为空)
另外,这里是logcat:
04-12 14:16:40.140: E/AndroidRuntime(19304): FATAL EXCEPTION: main
04-12 14:16:40.140: E/AndroidRuntime(19304): java.lang.NullPointerException
04-12 14:16:40.140: E/AndroidRuntime(19304): at com.example.sal.ProfileActivity$15.onPostExecute(ProfileActivity.java:1998)
04-12 14:16:40.140: E/AndroidRuntime(19304): at com.example.sal.ProfileActivity$15.onPostExecute(ProfileActivity.java:1)
04-12 14:16:40.140: E/AndroidRuntime(19304): at android.os.AsyncTask.finish(AsyncTask.java:602)
04-12 14:16:40.140: E/AndroidRuntime(19304): at android.os.AsyncTask.access$600(AsyncTask.java:156)
04-12 14:16:40.140: E/AndroidRuntime(19304): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
04-12 14:16:40.140: E/AndroidRuntime(19304): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 14:16:40.140: E/AndroidRuntime(19304): at android.os.Looper.loop(Looper.java:137)
04-12 14:16:40.140: E/AndroidRuntime(19304): at android.app.ActivityThread.main(ActivityThread.java:4575)
04-12 14:16:40.140: E/AndroidRuntime(19304): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 14:16:40.140: E/AndroidRuntime(19304): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 14:16:40.140: E/AndroidRuntime(19304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-12 14:16:40.140: E/AndroidRuntime(19304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-12 14:16:40.140: E/AndroidRuntime(19304): at dalvik.system.NativeStart.main(Native Method)