我正在尝试使用ImageLoader库将图像从互联网加载到网格视图中。但是,在imageManager.getLoader().load(fView.pic);
获取空指针异常。
有人可以告诉我我哪里错了。
StartApp课程:
public class StartApp extends Application {
private static ImageManager imageManager;
@Override
public void onCreate() {
super.onCreate();
LoaderSettings settings = new LoaderSettings();
settings.setDisconnectOnEveryCall(true);
settings.setCacheManager(new LruBitmapCache(this));
imageManager = new ImageManager(this, settings);
// // To clean the file cache
// imageManager.getFileManager().clean();
}
public static ImageManager getImageLoader() {
return imageManager;
}
}
活动类:
public class MainActivity extends Activity {
private GridView gridview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setPadding(10, 10, 10, 10);
gridview.setAdapter(new GridImageAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
扩展BaseAdapter的GridView适配器类:
public class GridImageAdapter extends BaseAdapter {
private Context context;
private int width = 270;
private int height = 270;
private int textFactor = 35;
private LayoutInflater layoutInflater;
private String[] nameEvents = {"image1","image2"};
private String[] urls = {"http://api.androidhive.info/images/sample.jpg","http://api.androidhive.info/images/sample.jpg" };
ImageManager imageManager;
ImageTagFactory imageTagFactory;
public GridImageAdapter(Context mContext) {
context = mContext;
layoutInflater = LayoutInflater.from(context);
/*Image Loader*/
imageManager = StartApp.getImageLoader();
imageTagFactory = ImageTagFactory.newInstance(context, R.drawable.ic_launcher);
imageTagFactory.setErrorImageId(R.drawable.not_found);
}
public int getCount() {
return nameEvents.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
FriendView fView = new FriendView();
int imagePadding = 10;
if (convertView == null) {
convertView = new View(context);
convertView = layoutInflater.inflate(R.layout.grid_cell_layout,
null);
// convertView.setBackgroundColor(Color.DKGRAY);
convertView.setPadding(3, 0, imagePadding - 1, imagePadding);
convertView.setBackgroundResource(R.drawable.background_shadow);
convertView
.setLayoutParams(new GridView.LayoutParams(width, height));
} else {
fView = (FriendView) convertView.getTag();
}
fView.pic = (ImageView) convertView.findViewById(R.id.image);
/* height has to be reduced to display the text */
fView.pic.setLayoutParams(new LinearLayout.LayoutParams(width, height
- textFactor));
fView.pic.setAdjustViewBounds(true);
fView.pic.setScaleType(ImageView.ScaleType.FIT_XY);
//((StartApp) context.getApplicationContext()).getImageLoader().getLoader().load(imageView);
fView.name = (TextView) convertView.findViewById(R.id.title);
fView.name.setTextColor(Color.BLACK);
convertView.setTag(fView);
fView.name.setText(nameEvents[position]);
// GridImageAdapter.imageLoader.DisplayImage(urls[position], fView.pic);
((ImageView) fView.pic).setTag(imageTagFactory.build(urls[position],context));
imageManager.getLoader().load(fView.pic);
return convertView;
}
static class FriendView {
TextView name;
ImageView pic;
}
}
提前致谢。
答案 0 :(得分:0)
首先,不要在适配器中创建另一个imagemanager对象。做StartApp.getImageLoader()。getLoader()。load(fView.pic);
第二个关闭,如果事物为空,则可以通过添加一些空检查和/或记录来轻松找到空指针。之后我们可以告诉哪个对象为空。
示例:
if(StartApp.getImageLoader().getLoader() != null) {
StartApp.getImageLoader().getLoader().load(fView.pic);
}
答案 1 :(得分:0)
看起来您没有正确设置应用程序类 确保在清单中声明Application类,如:
<application android:name="class" ...
您应该验证onCreate是否已执行,可能会记录一些日志。 您永远不应该检查ImageManager是否为null,如果它为null则存在错误