Image not loading using the Novoda / ImageLoader

ε祈祈猫儿з 提交于 2019-12-11 16:21:18

问题


I'm trying to load images from the internet using the ImageLoader library into a grid view. But, getting a null pointer exception at imageManager.getLoader().load(fView.pic);.

Can someone tell me where i'm wrong.

StartApp class :

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;
    }
}

Activity class:

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;
    }

}

GridView adapter class which extends the BaseAdapter:

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;
    }
}

Thanks in advance.


回答1:


First off, don't create another object of imagemanager in your adapter. Do StartApp.getImageLoader().getLoader().load(fView.pic);

Second off, a null pointer can easily be found by adding some null checks and/or logging if things are null or not. After that we can tell what object is null.

Example:

if(StartApp.getImageLoader().getLoader() != null) {
    StartApp.getImageLoader().getLoader().load(fView.pic);
}



回答2:


Looks like you are not setting correctly the application class Make sure to declare the Application class in the manifest like :

    <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



来源:https://stackoverflow.com/questions/14869769/image-not-loading-using-the-novoda-imageloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!