Android 从drawable或asset文件夹加载图像会触发java.lang.NullPointerException

Android 从drawable或asset文件夹加载图像会触发java.lang.NullPointerException,android,android-drawable,universal-image-loader,Android,Android Drawable,Universal Image Loader,编辑:我解决了这个问题,将UIL库更新为1.8.0,因为从资产文件夹添加图像的功能在早期版本中不可用。我确实错过了 我正在使用库在ListView中显示图像列表 如果图像不存在,我想从drawable或asset文件夹加载占位符 在库文档上,这是可接受的URI: String imageUri = "assets://image.png"; // from assets String imageUri = "drawable://" + R.drawable.image; // from d

编辑:我解决了这个问题,将UIL库更新为1.8.0,因为从资产文件夹添加图像的功能在早期版本中不可用。我确实错过了

我正在使用库在ListView中显示图像列表

如果图像不存在,我想从drawable或asset文件夹加载占位符

在库文档上,这是可接受的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?

我猜您确实检查了这里使用的URI

imageLoader.displayImage(文件前缀+图像路径,图像)

格式良好

我查看了库中的
BaseImageDownloader
类,了解了检索资产文件的方法,所有代码看起来都是有序的。下面是发生错误的地方。图像的选项为空,这意味着图像路径不正确

    Options options = new Options();
    options.inJustDecodeBounds = true;
    InputStream imageStream = imageDownloader.getStream(imageUri, displayOptions.getExtraForDownloader());
    try {
        BitmapFactory.decodeStream(imageStream, null, options);
    } finally {
        IoUtils.closeSilently(imageStream);
    }

    int scale = 1;
    int imageWidth = options.outWidth; //<-- this is where your error ocurrs
    int imageHeight = options.outHeight;

UIL只支持
内容://
资产://
可绘图://
开箱即用的方案,因为版本1.8.0

是什么版本的lib?什么是
file\u prefix
?我使用的是1.7.0,我刚刚查看了更改日志,并且从1.8.0开始添加了我使用的功能:(file\u prefix是
private String file\u prefix=“file://”;
@NOSTRA I更新为1.8.0,它的工作原理与charm@NOSTRA请添加您对该版本的建议作为答案,以便我可以接受。Cheers我是个白痴…从资源文件夹加载图像的功能是在库的1.8.0版本中添加的,而我使用的是1.7.0。我更新了库,一切正常。I v因为我可能需要你在另一个项目中建议的代码,所以我还是会给你打电话;)请查看以下问题:
    AssetManager mngr = getAssets();
    InputStream stream;
    try {
        stream = mngr.open("your_picture.png");

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = 2;// If you want to scale

        Bitmap bitmap = BitmapFactory.decodeStream(stream, new Rect(), o2);

        ImageView image = (ImageView) view.findViewById(R.id.image);
        image.setImageBitmap(bitmap);

    } catch (IOException e) {
        Log.e(TAG, "ERROR GETTING IMAGE", e);
    }