在全局定义中,我已经声明:private HashMap<String, Bitmap> map = new HashMap<String, Bitmap>();
在我的代码的其他部分,我连接到服务器并获取所需的信息。其中两个是图像地址(url)和图像ID。之后我下载图像,我想为它分配自己的 ID。这是我的代码:
private LinkedList<Bitmap> getFlagImages() {
InputStream is= null;
LinkedList<Bitmap> llBitmap = new LinkedList<Bitmap>();
for(int i = 0; i < flag.getTeamLogo44x44().size(); i++) {
String urlstr = flag.getTeamLogo44x44().get(i);
try {
HttpGet httpRequest = new HttpGet(urlstr);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
is = bufHttpEntity.getContent();
Bitmap bm = BitmapFactory.decodeStream(is);
llBitmap.add(bm);
map.put(flag.getTeamId().get(i), bm); // *Crash happens here
}catch ( MalformedURLException e ){
Log.d( "RemoteImageHandler", "Invalid URL passed" + urlstr );
}catch ( IOException e ){
Log.d( "RemoteImageHandler", "fetchImage IO exception: " + e );
}finally{
if(is != null) {
try{
is.close();
} catch(IOException e) {}
}
}
}
return llBitmap;
}
当我运行时,应用程序崩溃并且 logcat 显示空指针异常并指向行map.put(flag.getTeamId().get(i), bm);
任何建议将不胜感激。
//更新,我也用过map.put(flag.getTeamId().get(i), Bitmap.createBitmap(bm));
,但结果是一样的。