我想把位图图像放到ListView,
我使用这种方法:
public Drawable getAssetImage(Context context, String filename) throws IOException {
AssetManager assets = getAssets();
InputStream buffer = new BufferedInputStream((assets.open("flags/" + filename + ".png")));
Bitmap bitmap = BitmapFactory.decodeStream(buffer);
return new BitmapDrawable(bitmap);
}
然后,我进入了hashmap:
temp.put("Icon", getAssetImage(this, "Cyprus"));
我在资产/旗帜中有我的图标
这里有错误日志:
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): FATAL EXCEPTION: main
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): java.lang.NullPointerException
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at com.project.castroll.DataBaseHelper.getAssetImage(DataBaseHelper.java:111)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at com.project.castroll.DataBaseHelper.selectAll(DataBaseHelper.java:96)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at com.project.castroll.CastrollApp.showKwalifikacja(CastrollApp.java:282)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at com.project.castroll.CastrollApp$2.onClick(CastrollApp.java:255)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at android.view.View.performClick(View.java:2485)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at android.view.View$PerformClick.run(View.java:9080)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at android.os.Handler.handleCallback(Handler.java:587)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at android.os.Looper.loop(Looper.java:123)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at java.lang.reflect.Method.invoke(Method.java:507)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-13 12:39:12.236: ERROR/AndroidRuntime(10934): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
不要在HashMap中存储Drawable使用你自己的SimpleAdapter实现 (因为hashmap中的“Icon”只放置文件名)
public class MySimpleAdapter extends SimpleAdapter{
Context localcontext = null;
public MySimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
localcontext = context;
}
@Override
public void setViewImage(ImageView v, String value){
try {
v.setImageDrawable(Drawable.createFromStream(localcontext.getAssets().open("flags/" + value + ".png"), null));
} catch (IOException e) {
e.printStackTrace();
}
}
//EDIT ... he ask how to add odd/even row with diff colours
private int[] colors = new int[] { 0xe0e0e0ff, 0x404040ff };
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
并使用MySimpleAdapter而不是SimpleAdapter