为啥会发生此错误? java.lang.RuntimeException:ImageLoader 必须在使用前通过配置初始化

Posted

技术标签:

【中文标题】为啥会发生此错误? java.lang.RuntimeException:ImageLoader 必须在使用前通过配置初始化【英文标题】:Why this Error occurred? java.lang.RuntimeException: ImageLoader must be init with configuration before using为什么会发生此错误? java.lang.RuntimeException:ImageLoader 必须在使用前通过配置初始化 【发布时间】:2012-09-08 05:32:52 【问题描述】:

我从 gitHub(from Here) 下载了一个 ImageDownloader 代码 现在,当我尝试从我的网络服务下载图像时,我收到了这个运行时异常“ImageLoader must be init with configuration before using”。

我想不通。

这是我的适配器:

public class CustomAdapter extends BaseAdapter 

    private Context context;
    private ArrayList<String> logo_URL;

    private ListContent listContent;
    private ArrayList< TeamDataClass> teamdata = null;
    private ArrayList<EventDataClass> eventdata = null;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    public CustomAdapter(Context context,ArrayList<String> logo_URL,ArrayList<TeamDataClass> teamdata, ArrayList<EventDataClass> eventdata) 
    
        this.context = context;
        this.logo_URL = logo_URL;
        this.teamdata = teamdata;
        this.eventdata = eventdata;
        options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheOnDisc()
        .imageScaleType(ImageScaleType.EXACT)
        .build();
        imageLoader= ImageLoader.getInstance();
     

    //@Override
    public int getCount() 
    
        return teamdata.size()/2;
    

    //@Override
    public Object getItem(int arg0) 
    
        return arg0;
    

    //@Override
    public long getItemId(int arg0) 
    
        return arg0;
    

    //@Override
    public View getView(int arg0, View convertview, ViewGroup arg2) 
    
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        if (convertview == null ) 
        
            convertview = inflater.inflate(R.layout.custom_list_score, null);
            listContent = new ListContent();
            listContent.team_logo2 = (ImageView) convertview.findViewById(R.id.team_logo2);
            listContent.team_logo1 = (ImageView) convertview.findViewById(R.id.team_logo1);
            listContent.progress_team1=(ProgressBar)convertview.findViewById(R.id.progress_team_logo1);
            listContent.progress_team2=(ProgressBar)convertview.findViewById(R.id.progress_team_logo2);

            convertview.setTag(listContent);
        
        else 
        
            listContent = (ListContent) convertview.getTag();
        
        listContent.status.setText(eventdata.get(arg0).status);
        int pos=arg0*2;
        imageLoader.displayImage(logo_URL.get(pos),listContent.team_logo1, options,new ImageLoadingListener() 
        
            //@Override
            public void onLoadingStarted()
            
                listContent.progress_team1.setVisibility(View.VISIBLE);
            

            //@Override
            public void onLoadingFailed(FailReason failReason)
            
                String message = null;
                switch (failReason) 
                
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

                listContent.progress_team1.setVisibility(View.GONE);
                listContent.team_logo1.setImageResource(android.R.drawable.ic_delete);
            

            //@Override
            public void onLoadingComplete(Bitmap loadedImage)
            
                listContent.progress_team1.setVisibility(View.GONE);
                Animation anim = AnimationUtils.loadAnimation(context, R.anim.fade_in);
                listContent.team_logo1.setAnimation(anim);
                anim.start();
            

            //@Override
            public void onLoadingCancelled()
            
                // Do nothing
            

            public void onLoadingComplete() 

            
        ); 
        imageLoader.displayImage(logo_URL.get(pos+1),listContent.team_logo2, options,new ImageLoadingListener() 
        
            //@Override
            public void onLoadingStarted()
            
                listContent.progress_team2.setVisibility(View.VISIBLE);
            

            //@Override
            public void onLoadingFailed(FailReason failReason)
            
                String message = null;
                switch (failReason) 
                
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

                listContent.progress_team2.setVisibility(View.GONE);
                listContent.team_logo2.setImageResource(android.R.drawable.ic_delete);
            

            //@Override
            public void onLoadingComplete(Bitmap loadedImage)
            
                listContent.progress_team2.setVisibility(View.GONE);
                Animation anim = AnimationUtils.loadAnimation(context, R.anim.fade_in);
                listContent.team_logo2.setAnimation(anim);
                anim.start();
            

            //@Override
            public void onLoadingCancelled()
            
                // Do nothing
            

            public void onLoadingComplete() 

            
        ); 

        return convertview;
    
    private class ListContent
    
        ImageView team_logo1, team_logo2;
        ProgressBar progress_team1,progress_team2;
    

这是我的日志:

FATAL EXCEPTION: main
java.lang.RuntimeException: ImageLoader must be init with configuration before using
at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:164)
at com.huskerit.score.CustomAdapter.getView(CustomAdapter.java:100)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:1554)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1288)
at android.widget.ListView.onMeasure(ListView.java:1199)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

Android - ImageLoader must be init with configuration before using in UIL的可能重复 【参考方案1】:
protected ImageLoader imageLoader;

@Override
public void onCreate(Bundle savedInstanceState) 
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(this));

【讨论】:

工作完美。老板【参考方案2】:

我在构造函数中添加了这一行,它对我有用...

imageLoader.init(ImageLoaderConfiguration.createDefault(context));

【讨论】:

是的。这是对的。我已经包含在我的构造函数中了。

以上是关于为啥会发生此错误? java.lang.RuntimeException:ImageLoader 必须在使用前通过配置初始化的主要内容,如果未能解决你的问题,请参考以下文章

为啥在我尝试构建此 mxml 文件时会发生此非描述性错误?

为啥会发生此错误? java.lang.RuntimeException:ImageLoader 必须在使用前通过配置初始化

Spark.read.csv 错误:java.io.IOException:权限被拒绝

为啥存储此向量时会出现分段错误?

java.lang.ClassCastException 为啥?

为啥在此代码中调用虚拟方法时会出现分段错误?