编辑:我解决了将 UIL 库更新到 1.8.0 的问题,因为从资产文件夹添加图像的功能在早期版本中不可用。我确实错过了。
我正在使用通用图像加载器库在 ListView 中显示图像列表
如果图像不存在,我想从可绘制或资产文件夹加载占位符。
在库文档中,这是接受的 URI:
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables
但使用任一选项都会触发java.lang.NullPointerException
这是我加载图像的 onResume 方法:
@Override
public void onResume() {
super.onResume();
//open connection to db
db = new DBAdapter(this);
db.open();
// get all defects for this unit
defectList = db.getAllDefectsByUnit(unit_id);
// create an array adapter and let it to display our row
defects = new SimpleCursorAdapter(this, R.layout.defect_row, defectList, new String[] { "defect", "image_path" }, new int[] { R.id.defect, R.id.image }, 0);
//set custom view using ViewBinder
SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
//int placeholder_id = getResources().getIdentifier("placeholder", "drawable", getPackageName());
//get column name
String name = cursor.getColumnName(columnIndex);
//for the thumbnail column,if we have an image replace the placeholder
if ("image_path".equals(name)) {
ImageView image = (ImageView) view.findViewById(R.id.image);
//Bitmap thumbnail;
String image_path = cursor.getString(columnIndex);
Log.i("image_path ->", image_path);
if (!image_path.equalsIgnoreCase("")) {
// Load and display image asynchronously
imageLoader.displayImage(file_prefix + image_path, image);
} else {
String imageUri = "drawable://" + R.drawable.placeholder; // drawable
//image.setImageResource(placeholder_id);
// Load and display image asynchronously
imageLoader.displayImage(imageUri, image);
}
return true;
}
//for the defect column, just add the text to the view
if ("defect".equals(name)) {
String defect_text = cursor.getString(columnIndex);
TextView defect_holder = (TextView) view.findViewById(R.id.defect);
defect_holder.setText(defect_text);
return true;
}
return false;
}
};
defects.setViewBinder(binder);
setListAdapter(defects);
}//onResume
从文件加载很好。
如果我尝试使用常见的 Android 方法加载占位符图像:
int placeholder_id = getResources().getIdentifier("placeholder", "drawable", getPackageName());
image.setImageResource(placeholder_id);
然后图像加载正常(即使我在这里提到了另一个问题)
这是日志:
`03-07 15:58:07.141: E/ImageLoader(22950): null
03-07 15:58:07.141: E/ImageLoader(22950): java.lang.NullPointerException
03-07 15:58:07.141: E/ImageLoader(22950): at com.nostra13.universalimageloader.core.ImageDecoder.computeImageScale(ImageDecoder.java:121)
03-07 15:58:07.141: E/ImageLoader(22950): at com.nostra13.universalimageloader.core.ImageDecoder.getBitmapOptionsForImageDecoding(ImageDecoder.java:104)
03-07 15:58:07.141: E/ImageLoader(22950): at com.nostra13.universalimageloader.core.ImageDecoder.decode(ImageDecoder.java:82)
03-07 15:58:07.141: E/ImageLoader(22950): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeWithOOMHandling(LoadAndDisplayImageTask.java:252)
03-07 15:58:07.141: E/ImageLoader(22950): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:235)
03-07 15:58:07.141: E/ImageLoader(22950): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:211)
03-07 15:58:07.141: E/ImageLoader(22950): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:128)
03-07 15:58:07.141: E/ImageLoader(22950): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
03-07 15:58:07.141: E/ImageLoader(22950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-07 15:58:07.141: E/ImageLoader(22950): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-07 15:58:07.141: E/ImageLoader(22950): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-07 15:58:07.141: E/ImageLoader(22950): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-07 15:58:07.141: E/ImageLoader(22950): at java.lang.Thread.run(Thread.java:856)`
我错过了什么?我是否传递了格式错误的 URI?