0

我正在尝试使用 ImageLoader 库将来自 Internet 的图像加载到网格视图中。但是,在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;
    }
}

提前致谢。

4

2 回答 2

0

首先,不要在适配器中创建另一个 imagemanager 对象。做 StartApp.getImageLoader().getLoader().load(fView.pic);

其次,可以通过添加一些空检查和/或记录是否为空来轻松找到空指针。之后我们可以判断什么对象是空的。

例子:

if(StartApp.getImageLoader().getLoader() != null) {
    StartApp.getImageLoader().getLoader().load(fView.pic);
}
于 2013-02-15T16:26:46.800 回答
0

看起来您没有正确设置应用程序类确保在清单中声明应用程序类,如:

    <application android:name="class" ...

You should verify that the onCreate it is executed, maybe put some log. You should never check for the ImageManager to be null, if it is null there is something wrong

于 2013-05-03T20:42:13.663 回答