通用ImageLoader - 视图不能为空

时间:2014-12-29 08:08:38

标签: android android-fragments universal-image-loader

我希望在ListView中显示图标,然后使用libs Universal Imageloader。但是给我错误。 请给我一个解决方案或编辑我的代码。

感谢您的帮助!

抱歉我的英语不好。

代码片段

public class InfoTeacherFragment extends Fragment {
    ProgressDialog pDialog;
    Context mContext;
    Vector<ArrayList<String>> data;
    ArrayList<String> Name;
    ArrayList<String> School;
    ArrayList<String> Link;
    ArrayList<String> Icon;
    ListView lvItem;
    DisplayImageOptions options;

    public InfoTeacherFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_mission)
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
                .cacheOnDisk(true).considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.teacherinfo_layout,
                container, false);
        mContext = container.getContext();
        lvItem = (ListView) rootView.findViewById(R.id.lvTeacher);
        if (Utils.isOnline(mContext) == true
                && Utils.KEY_CHECK_SUCCESS.equals("SUCCESS")) {
            new LoadData().execute();

            lvItem.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapter, View view,
                        int position, long id) {

                    Intent intent = new Intent(mContext, NewsItemView.class);
                    if (Link != null) {
                        intent.putExtra("URL", Link.get(position));
                        startActivity(intent);
                    }
                }
            });
        } else if (Utils.isOnline(mContext) == true
                && Utils.KEY_CHECK_SUCCESS.equals("FAIL")) {
            Utils.MyToast(Utils.KEYWORK_ERROR[0], R.drawable.warning_icon,
                    mContext, false);
        }
        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AnimateFirstDisplayListener.displayedImages.clear();
    }

    class LoadData extends AsyncTask<Void, Void, String> {
        private String Url = "http://scv.udn.vn/dhdn/trdhsp/page/1";

        @Override
        protected void onPreExecute() {
            pDialog = ProgressDialog.show(mContext, "", Utils.KEYWORK_ERROR[2]);
            data = new Vector<ArrayList<String>>();
            Name = new ArrayList<String>();
            School = new ArrayList<String>();
            Link = new ArrayList<String>();
            Icon = new ArrayList<String>();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... params) {
            data = Utils.getInformationTeacher(Url);
            if (!(data.isEmpty())) {
                return Utils.KEY_CHECK_SUCCESS;
            } else {
                return Utils.KEY_CHECK_FAIL;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (result.equals(Utils.KEY_CHECK_SUCCESS)) {
                pDialog.dismiss();
                Name = data.get(0);
                School = data.get(1);
                Icon = data.get(2);
                Link = data.get(3);
                ((ListView) lvItem).setAdapter(new InfoTeacher(mContext, Name,
                        School, Icon));
            } else if (result.equals(Utils.KEY_CHECK_FAIL)) {
                pDialog.dismiss();
                Utils.MyToast(Utils.KEYWORK_ERROR[0], R.drawable.warning_icon,
                        mContext, false);
            }
            super.onPostExecute(result);
        }
    }

    class InfoTeacher extends BaseAdapter {
        private Context context;
        private ArrayList<String> Icon = new ArrayList<String>();
        private ArrayList<String> Name = new ArrayList<String>();
        private ArrayList<String> School = new ArrayList<String>();
        private LayoutInflater inflater;
        ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

        public InfoTeacher(Context context, ArrayList<String> Name,
                ArrayList<String> School, ArrayList<String> Icon) {
            this.context = context;
            this.Name = Name;
            this.Icon = Icon;
            this.School = School;
            inflater = LayoutInflater.from(getActivity());
        }

        @Override
        public int getCount() {
            return Name.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup arg2) {
            View rowView = view;
            ViewHolder holder;
            if (rowView == null) {
                holder = new ViewHolder();
                // inflater = (LayoutInflater) context
                // .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                rowView = inflater.inflate(R.layout.teacher_layout, null);
                holder.Name = (TextView) rowView
                        .findViewById(R.id.tv_NameTeacher);
                holder.School = (TextView) rowView.findViewById(R.id.tv_School);
                holder.ic_naviga = (ImageView) rowView
                        .findViewById(R.id.ivNaviga);
                holder.icon = (ImageView) rowView
                        .findViewById(R.id.iv_IconTeacher);
                rowView.setTag(holder);
            } else
                holder = (ViewHolder) view.getTag();
            final String mIcon = Icon.get(position);
            holder.Name.setText(Name.get(position).toString());
            holder.School.setText(School.get(position).toString());
            ImageLoader.getInstance().displayImage(mIcon, holder.icon, options,
                    animateFirstListener);
            return rowView;
        }

    }

    static class ViewHolder {
        ImageView icon;
        TextView Name;
        TextView School;
        ImageView ic_naviga;
    }

    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
    }

错误

12-29 07:54:34.400: E/AndroidRuntime(7687): FATAL EXCEPTION: main
12-29 07:54:34.400: E/AndroidRuntime(7687): java.lang.IllegalArgumentException: view must not be null
12-29 07:54:34.400: E/AndroidRuntime(7687):     at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:70)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:50)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at com.nostra13.universalimageloader.core.imageaware.ImageViewAware.<init>(ImageViewAware.java:44)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:365)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:340)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.readnews.main.InfoTeacherFragment$InfoTeacher.getView(InfoTeacherFragment.java:196)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.ListView.fillDown(ListView.java:674)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.ListView.fillFromTop(ListView.java:735)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.ListView.layoutChildren(ListView.java:1652)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.View.layout(View.java:14008)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.View.layout(View.java:14008)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.View.layout(View.java:14008)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:678)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.View.layout(View.java:14008)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.View.layout(View.java:14008)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.View.layout(View.java:14008)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.View.layout(View.java:14008)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.Choreographer.doFrame(Choreographer.java:532)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.os.Handler.handleCallback(Handler.java:725)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.os.Looper.loop(Looper.java:137)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at java.lang.reflect.Method.invoke(Method.java:511)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-29 07:54:34.400: E/AndroidRuntime(7687):     at dalvik.system.NativeStart.main(Native Method)

行代码错误:

ImageLoader.getInstance().displayImage(mIcon, holder.icon, options,
                        animateFirstListener);

1 个答案:

答案 0 :(得分:0)

您尚未初始化ImageLoader

在适配器中显示图像之前添加此代码:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
ImageLoader.getInstance().init(config);