我正在尝试实现一个库,其中我以JSON文件的形式获取图像(拇指)的URL。我将json解析为ArrayLists thmb,以字符串的形式保存缩略图图像的URL。 一切都很好用JSON和URL字符串。
我想在图库视图中加载缩略图。为此,我试图下载图像并将它们保存在ArrayList中。相同的代码是:
ArrayList<String> thmb;
ArrayList<Bitmap> b;
DownloadImageTask dit = new DownloadImageTask();
for(int j=0;j<thmb.size();j++){
Log.d("REQ","REQUESTING"+thmb.get(j));
dit.execute(thmb.get(j).toString());
Log.d("BR", "BITMAP RECIEVED");
Bitmap bit = null;
try{
bit=dit.get();
b.add(bit);
}catch(Exception e){
Log.d("LLL", e.getMessage());
}
}
班级下载图片任务:
public class DownloadImageTask extends AsyncTask <String, Bitmap, Bitmap> {
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap img = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
img = BitmapFactory.decodeStream(in);
return img;
} catch (Exception e) {
Log.e("Error", e.getMessage());
return null;
}
}
protected void onPostExecute(Bitmap imx) {
super.onPostExecute(imx);
}
}
我无法在ArrayList中保存位图,应用程序意外停止!任何人都可以告诉我为什么会这样。 LogCat说:
12-22 18:48:07.398: D/BR(6129): BITMAP RECIEVED
12-22 18:48:08.349: D/AndroidRuntime(6129): Shutting down VM
12-22 18:48:08.349: W/dalvikvm(6129): threadid=1: thread exiting with uncaught exception (group=0x415e9d88)
12-22 18:48:08.359: E/AndroidRuntime(6129): FATAL EXCEPTION: main
12-22 18:48:08.359: E/AndroidRuntime(6129): Process: com.example.videogallery, PID: 6129
12-22 18:48:08.359: E/AndroidRuntime(6129): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.videogallery/com.example.videogallery.Stream}: java.lang.NullPointerException: println needs a message
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.os.Looper.loop(Looper.java:212)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.main(ActivityThread.java:5135)
12-22 18:48:08.359: E/AndroidRuntime(6129): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 18:48:08.359: E/AndroidRuntime(6129): at java.lang.reflect.Method.invoke(Method.java:515)
12-22 18:48:08.359: E/AndroidRuntime(6129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-22 18:48:08.359: E/AndroidRuntime(6129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-22 18:48:08.359: E/AndroidRuntime(6129): at dalvik.system.NativeStart.main(Native Method)
12-22 18:48:08.359: E/AndroidRuntime(6129): Caused by: java.lang.NullPointerException: println needs a message
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.util.Log.println_native(Native Method)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.util.Log.d(Log.java:139)
12-22 18:48:08.359: E/AndroidRuntime(6129): at com.futurristic.videogallery.Stream.onCreate(Stream.java:90)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.Activity.performCreate(Activity.java:5231)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
12-22 18:48:08.359: E/AndroidRuntime(6129): ... 11 more
答案 0 :(得分:1)
你有一个具有null的Log语句。所以空指针。看看Stream.java类的第90行。
更多帮助:
NullPointerException : println needs a message in android
NullPointerException: println needs a message
Android java.lang.NullPointerException: println needs a message
答案 1 :(得分:0)
基于Logcat,你在thmb.get(j)中有NPE,所以要显示它填充的部分。