修复了ImageLoader的警告:“尝试初始化之前已经初始化的ImageLoader”

时间:2014-02-05 14:21:02

标签: java android universal-image-loader

在我的应用中,我有一些包含一些项目的列表视图。每个项目都有一个从远程网址下载的img。

我使用ImageLoader库将每个图像加载到我的应用程序中。

我的列表视图构建得很好,我有所有图像,但我在LogCat中有一个警告:

  

02-05 15:16:17.938:W / ImageLoader(17363):尝试初始化   之前已经初始化的ImageLoader。要重新初始化   带有新配置的ImageLoader调用ImageLoader.destroy()   第一

我不知道如何解决这个问题?

这是我的适配器的代码:

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;

    } 

3 个答案:

答案 0 :(得分:15)

当您尝试初始化ImageLoader时,会发生此错误。

在我的应用程序中,我在不同的活动中以两种方式初始化了ImageLoader:

1:

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

2:

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

在我的应用中,我使用ViewPager导致此错误,这就是原因:

重要的是要知道当您在ViewPager中打开标签时,它会自动在后台加载第二个标签。

假设这个初始化是在第一个选项卡上设置的。因此,当您转到第二个和第三个选项卡然后返回第二个选项卡,然后加载第一个选项卡并尝试再次初始化ImageLoader。

您需要做的是检查它是否已初始化

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

答案 1 :(得分:6)

ImageLoader必须只初始化一次。每次调用getView()方法时都会初始化。在从应用程序

扩展的类中,可以更好地初始化imageLoder
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);
    }
}

之后的适配器

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;

} 

答案 2 :(得分:0)

您的适配器有问题。试一试:

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;

}

亲爱的用户,您需要参考以下链接:http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ 检查自定义适配器的实现方式..