I’m trying to use the universal image loader to load images into a gridview but my app seems to be crashing right when the activity loads and I cant identify the problem. I’m loading the images from an array then calling the image adapter to populate them.
All help will be greatly appreciated 🙂
public class MainActivity extends Activity {
String[] imageUrls;
DisplayImageOptions options;
protected ImageLoader imageLoader = ImageLoader.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
imageUrls = bundle.getStringArray(Extra.IMAGES);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter());
}
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
}
}
Logcat Error:
01-04 14:43:40.990: E/AndroidRuntime(11510): FATAL EXCEPTION: main
01-04 14:43:40.990: E/AndroidRuntime(11510): java.lang.RuntimeException: ImageLoader must be init with configuration before using
01-04 14:43:40.990: E/AndroidRuntime(11510): at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:175)
01-04 14:43:40.990: E/AndroidRuntime(11510): at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:131)
01-04 14:43:40.990: E/AndroidRuntime(11510): at com.example.breaktheglass.ImageGridActivity$ImageAdapter.getView(ImageGridActivity.java:96)
So I added this:
ImageLoader.init(ImageLoaderConfiguration.createDefault(this));
before I called the imageloader (below the .getInstance) but then it just gives me this error:
Syntax error on token "init", Identifier expected after this token
I’m not quite sure where to go from here..
Just change your code sequence like this,
Let me know what happen..
For more details, look at https://github.com/nostra13/Android-Universal-Image-Loader