Fix a warning of ImageLoader : “Try to initialize ImageLoader which had already been initialized before”

落爺英雄遲暮 提交于 2019-12-09 07:13:23

问题


In my app, i have a listview with some items. Each item has a img which is downloaded from distant url.

I use ImageLoader library to load each image into my app.

My listview is well built, i have all images, but i have a warning in LogCat :

02-05 15:16:17.938: W/ImageLoader(17363): Try to initialize ImageLoader which had already been initialized before. To re-init ImageLoader with new configuration call ImageLoader.destroy() at first.

I don't know how to fix this issue ?

Here the code of my adapter :

public class ArticleListAdapterHome extends ArrayAdapter<ArticleHome> {

    Typeface faceLight = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
    Typeface faceBold = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf");

    public ArticleListAdapterHome(Activity activity, List<ArticleHome> articles) {
        super(activity, 0, articles);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Activity activity = (Activity) getContext();

        LayoutInflater inflater = activity.getLayoutInflater();

        View rowView;
        ArticleHome article = getItem(position);

        if (position == 0) {

            rowView = inflater.inflate(R.layout.item_ligne_home_premier, null);

            ////////////////////////////////////////////////////////
            ///// IMAGE
            ///////////////////////////////////////////////////////
            ImageView imgfirst = (ImageView) rowView.findViewById(R.id.imgimg) ;

            DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())
            .defaultDisplayImageOptions(options)
            .threadPriority(Thread.MAX_PRIORITY)
            .denyCacheImageMultipleSizesInMemory()
            .memoryCacheSize(2 * 1024 * 1024)
            .build();

            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.init(config); 
            imageLoader.displayImage(article.getImage(), imgfirst);

            ListView listV = (ListView)parent;
            boolean pauseOnScroll = false; // or true
            boolean pauseOnFling = true; // or false
            PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
            listV.setOnScrollListener(listener);

            ////////////////////////////////////////////////////////
            ///// TITLE
            ///////////////////////////////////////////////////////
            TextView textView = (TextView) rowView.findViewById(R.id.titlepremier);     
            textView.setText(article.getTitle());
            textView.setTypeface(faceLight);
        }
        else {
            rowView = inflater.inflate(R.layout.item_ligne_home, null);

            ////////////////////////////////////////////////////////
            ///// IMAGE
            ///////////////////////////////////////////////////////
            ImageView img = (ImageView) rowView.findViewById(R.id.imgimg) ;

            DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())
            .defaultDisplayImageOptions(options)
            .threadPriority(Thread.MAX_PRIORITY)
            .denyCacheImageMultipleSizesInMemory()
            .memoryCacheSize(2 * 1024 * 1024)
            .build();

            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.init(config); 
            imageLoader.displayImage(article.getImage(), img);

            ListView listV = (ListView)parent;
            boolean pauseOnScroll = false; // or true
            boolean pauseOnFling = true; // or false
            PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
            listV.setOnScrollListener(listener);

            ////////////////////////////////////////////////////////
            ///// TITLE
            ///////////////////////////////////////////////////////
            TextView title = (TextView) rowView.findViewById(R.id.titlearticleothers);  
            title.setText(article.getTitle());
            title.setTypeface(faceBold);

            ////////////////////////////////////////////////////////
            ///// DESCRIPTION
            ///////////////////////////////////////////////////////
            TextView desc = (TextView) rowView.findViewById(R.id.descriptionarticleothers);
            desc.setText(article.getDescription());
            desc.setTypeface(faceLight);

            ////////////////////////////////////////////////////////
            ///// DATE
            ///////////////////////////////////////////////////////
            TextView date = (TextView) rowView.findViewById(R.id.date);
            String pubDate = article.getPubDate();
            SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss Z", Locale.ENGLISH);
            Date pDate;
            try {
                pDate = df.parse(pubDate);
                pubDate = DateUtils.getDateDifference(pDate);
                pubDate = pubDate.replace("-", "");
            } catch (ParseException e) {
                Log.e("DATE PARSING", "Error parsing date..");
                pubDate = "published by " + article.getAuthor();
            }
            date.setText("Il y a " +pubDate);

        }

        return rowView;

    } 

回答1:


This error occurs when you are trying to initialize ImageLoader when it is already initialized.

In my app I initialized ImageLoader two ways in different activities:

1:

imageLoader.init(ImageLoaderConfiguration.createDefault(this.getSherlockActivity()));

2:

initImageLoader(); //where this function contained the configuration settings.

In my app I am using a ViewPager which causes this error and this is why:

It's important to know that when you open a tab in the ViewPager it automatically loads the second tab in the background.

Let's say this initialization was set on the first tab. So when you go to the second and the third tab then you go back to the second tab then the first one is loaded and it tries to initialize ImageLoader again.

What you need to do is check if it was initialized

if (!imageLoader.isInited()) {
    initImageLoader();
 }



回答2:


ImageLoader must be initializated only once. You initializate every time when getView() method is called. It's could be better initializate imageLoder in your class extended from application

public class MyApp extends Application {

    public void onCreate() {
        super.onCreate();
        ContextHolder.context = getApplicationContext();

        // Create global configuration and initialize ImageLoader with this configuration
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .memoryCache(new UsingFreqLimitedMemoryCache(5 * 1024 * 1024)) // 5 Mb (delete most not used image)
        .build();
        ImageLoader.getInstance().init(config);
    }
}

Your adapter after that

public class ArticleListAdapterHome extends ArrayAdapter<ArticleHome> {

Typeface faceLight = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
Typeface faceBold = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf");
private ImageLoader imageLoader;

public ArticleListAdapterHome(Activity activity, List<ArticleHome> articles) {
    super(activity, 0, articles);
    imageLoader = ImageLoader.getInstance();
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Activity activity = (Activity) getContext();

    LayoutInflater inflater = activity.getLayoutInflater();

    View rowView;
    ArticleHome article = getItem(position);

    if (position == 0) {

        rowView = inflater.inflate(R.layout.item_ligne_home_premier, null);

        ////////////////////////////////////////////////////////
        ///// IMAGE
        ///////////////////////////////////////////////////////
        ImageView imgfirst = (ImageView) rowView.findViewById(R.id.imgimg) ;

        DisplayImageOptions options = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisc(true)
        .build();

        imageLoader.displayImage(article.getImage(), imgfirst);

        ListView listV = (ListView)parent;
        boolean pauseOnScroll = false; // or true
        boolean pauseOnFling = true; // or false
        PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
        listV.setOnScrollListener(listener);

        ////////////////////////////////////////////////////////
        ///// TITLE
        ///////////////////////////////////////////////////////
        TextView textView = (TextView) rowView.findViewById(R.id.titlepremier);     
        textView.setText(article.getTitle());
        textView.setTypeface(faceLight);
    }
    else {
        rowView = inflater.inflate(R.layout.item_ligne_home, null);

        ////////////////////////////////////////////////////////
        ///// IMAGE
        ///////////////////////////////////////////////////////
        ImageView img = (ImageView) rowView.findViewById(R.id.imgimg) ;

        DisplayImageOptions options = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisc(true)
        .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())
        .defaultDisplayImageOptions(options)
        .threadPriority(Thread.MAX_PRIORITY)
        .denyCacheImageMultipleSizesInMemory()
        .memoryCacheSize(2 * 1024 * 1024)
        .build();

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(config); 
        imageLoader.displayImage(article.getImage(), img);

        ListView listV = (ListView)parent;
        boolean pauseOnScroll = false; // or true
        boolean pauseOnFling = true; // or false
        PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
        listV.setOnScrollListener(listener);

        ////////////////////////////////////////////////////////
        ///// TITLE
        ///////////////////////////////////////////////////////
        TextView title = (TextView) rowView.findViewById(R.id.titlearticleothers);  
        title.setText(article.getTitle());
        title.setTypeface(faceBold);

        ////////////////////////////////////////////////////////
        ///// DESCRIPTION
        ///////////////////////////////////////////////////////
        TextView desc = (TextView) rowView.findViewById(R.id.descriptionarticleothers);
        desc.setText(article.getDescription());
        desc.setTypeface(faceLight);

        ////////////////////////////////////////////////////////
        ///// DATE
        ///////////////////////////////////////////////////////
        TextView date = (TextView) rowView.findViewById(R.id.date);
        String pubDate = article.getPubDate();
        SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss Z", Locale.ENGLISH);
        Date pDate;
        try {
            pDate = df.parse(pubDate);
            pubDate = DateUtils.getDateDifference(pDate);
            pubDate = pubDate.replace("-", "");
        } catch (ParseException e) {
            Log.e("DATE PARSING", "Error parsing date..");
            pubDate = "published by " + article.getAuthor();
        }
        date.setText("Il y a " +pubDate);

    }

    return rowView;

} 



回答3:


There something wrong with your adapter. Just a try:

public class ArticleListAdapterHome extends ArrayAdapter<ArticleHome> {

private Activity activity;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;

Typeface faceLight = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
Typeface faceBold = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf");

public ArticleListAdapterHome(Activity activity, List<ArticleHome> articles) {
    activity = a;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView;
    ArticleHome article = getItem(position);

    if (position == 0) {

        rowView = inflater.inflate(R.layout.item_ligne_home_premier, null);

        ////////////////////////////////////////////////////////
        ///// IMAGE
        ///////////////////////////////////////////////////////
        ImageView imgfirst = (ImageView) rowView.findViewById(R.id.imgimg) ;
        imageLoader.displayImage(article.getImage(), imgfirst);

        ListView listV = (ListView)parent;
        boolean pauseOnScroll = false; // or true
        boolean pauseOnFling = true; // or false
        PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
        listV.setOnScrollListener(listener);

        ////////////////////////////////////////////////////////
        ///// TITLE
        ///////////////////////////////////////////////////////
        TextView textView = (TextView) rowView.findViewById(R.id.titlepremier);     
        textView.setText(article.getTitle());
        textView.setTypeface(faceLight);
    }
    else {
        rowView = inflater.inflate(R.layout.item_ligne_home, null);

        ////////////////////////////////////////////////////////
        ///// IMAGE
        ///////////////////////////////////////////////////////
        ImageView img = (ImageView) rowView.findViewById(R.id.imgimg) ;

        imageLoader.displayImage(article.getImage(), img);

        ListView listV = (ListView)parent;
        boolean pauseOnScroll = false; // or true
        boolean pauseOnFling = true; // or false
        PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
        listV.setOnScrollListener(listener);

        ////////////////////////////////////////////////////////
        ///// TITLE
        ///////////////////////////////////////////////////////
        TextView title = (TextView) rowView.findViewById(R.id.titlearticleothers);  
        title.setText(article.getTitle());
        title.setTypeface(faceBold);

        ////////////////////////////////////////////////////////
        ///// DESCRIPTION
        ///////////////////////////////////////////////////////
        TextView desc = (TextView) rowView.findViewById(R.id.descriptionarticleothers);
        desc.setText(article.getDescription());
        desc.setTypeface(faceLight);

        ////////////////////////////////////////////////////////
        ///// DATE
        ///////////////////////////////////////////////////////
        TextView date = (TextView) rowView.findViewById(R.id.date);
        String pubDate = article.getPubDate();
        SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss Z", Locale.ENGLISH);
        Date pDate;
        try {
            pDate = df.parse(pubDate);
            pubDate = DateUtils.getDateDifference(pDate);
            pubDate = pubDate.replace("-", "");
        } catch (ParseException e) {
            Log.e("DATE PARSING", "Error parsing date..");
            pubDate = "published by " + article.getAuthor();
        }
        date.setText("Il y a " +pubDate);

    }

    return rowView;

}

Dear user, you need to refer this link: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ Check how custom adapter is implemented..



来源:https://stackoverflow.com/questions/21579826/fix-a-warning-of-imageloader-try-to-initialize-imageloader-which-had-already

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