我正在尝试使用通用图像加载器将图像加载到网格视图中,但是当活动加载时我的应用程序似乎正在崩溃,我无法识别问题。我正在从数组中加载图像,然后调用图像适配器来填充它们。
所有帮助将不胜感激:)
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;
}
}
}
日志错误:
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)
所以我添加了这个:
ImageLoader.init(ImageLoaderConfiguration.createDefault(this));
在我调用 imageloader(在 .getInstance 下方)之前,它只是给了我这个错误:
Syntax error on token "init", Identifier expected after this token
我不太确定从这里去哪里..