我正在制作一个应用程序,它使用 AsyncTask 选择画廊的 3 张图像。我的 AsyncTask> 的内容是:
public class ShoppingGallery extends AsyncTask<Void, Void, List<Bitmap>> {
private Activity activity;
private static final String LOG_TAG = ShoppingGallery.class.getSimpleName();
private Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
private String[] projection = {MediaStore.Images.Thumbnails.DATA};
private Cursor imageCursor;
public ShoppingGallery(Activity activity){
this.activity = activity;
imageCursor = activity.getContentResolver().query(uri, projection, null, null, null);
}
@Override
protected List<Bitmap> doInBackground(Void... params) {
List<Bitmap> imgs = new ArrayList<>();
while(imageCursor.moveToNext()){
try {
if(imgs.size() < 3)
imgs.add(MediaStore.Images.Media.getBitmap(activity.getContentResolver(), imageCursor.getNotificationUri()));
} catch (IOException e) {
Log.e(LOG_TAG, "problem with the image loading: " + e);
}
}
return imgs;
}
这对我来说似乎没问题,但是当我运行我的程序时它崩溃并给出以下错误消息:08-13 11:14:11.662 22360-22360/com.example.jonas.shoppinglist E/ShoppingContacts:图像执行失败:
java.util.concurrent.ExecutionException:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String android.net.Uri.getScheme()'
所以,检测到问题。我的程序抱怨的行是:
imgs.add(MediaStore.Images.Media.getBitmap(activity.getContentResolver(), imageCursor.getNotificationUri()));
来源和解决方案是什么?