在适配器中的映像加载器侦听器中错误地初始化ViewHolder会导致崩溃

时间:2014-01-30 17:11:13

标签: java android listview android-adapter universal-image-loader

我正在尝试将ViewHolder放到我的ListView适配器上,但它会强制我的应用程序崩溃。没有图像,它运作良好。我正在使用Nostra13的Universal-Image-Loader。我什么时候需要定义图像加载器监听器,我不知道如何将它与我的ViewHolder链接。

我的适配器(仅限getView + ViewHolder):

static class ViewHolder {
    TextView animalView;
    TextView areaView;
    ImageView notfound;
    ImageView animalPic;
    ProgressBar indicator;
}

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

    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
        convertView = mLayoutInflater.inflate(R.layout.animal_row_item, null);
        holder = new ViewHolder();

        holder.animalView = (TextView) convertView.findViewById(R.id.animal_text);
        holder.areaView = (TextView) convertView.findViewById(R.id.area_text);
        holder.notfound = (ImageView) convertView.findViewById(R.id.notfoundimg);
        holder.animalPic = (ImageView) convertView.findViewById(R.id.animal_pic);
        holder.indicator = (ProgressBar) convertView.findViewById(R.id.progress);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();

    }

    final Animal animal = mAnimals.get(position);

    holder.notfound.setVisibility(View.INVISIBLE);
    holder.indicator.setVisibility(View.VISIBLE);
    holder.animalPic.setVisibility(View.INVISIBLE);

    //Setup a listener we can use to switch from the loading indicator to the Image once it's ready
    ImageLoadingListener listener = new ImageLoadingListener() {

        ViewHolder holder;

        @Override
        public void onLoadingStarted(String arg0, View arg1) {

        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {

        }

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
            holder.indicator.setVisibility(View.INVISIBLE);
            holder.animalPic.setVisibility(View.VISIBLE);
            holder.notfound.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onLoadingFailed(String arg0, View view, FailReason arg2) {
            holder.notfound.setVisibility(View.VISIBLE);
            holder.indicator.setVisibility(View.INVISIBLE);
            holder.animalPic.setVisibility(View.INVISIBLE);
        }

    };

    imageLoader.displayImage(animal.getImgUrl(), holder.animalPic, options, listener);
    holder.animalView.setText(animal.getAnimal());
    holder.areaView.setText(animal.getArea());

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

            Intent intent = new Intent(getContext(), MoreActivity.class);

            intent.putExtra("about", animal.getAbout());
            intent.putExtra("animal", animal.getAnimal());
            intent.putExtra("imgUrl", animal.getImgUrl());
            getContext().startActivity(intent);
        }
    });

    return convertView;
}

logcat的:

01-30 17:01:53.713: E/AndroidRuntime(12702): FATAL EXCEPTION: main
01-30 17:01:53.713: E/AndroidRuntime(12702): java.lang.NullPointerException
01-30 17:01:53.713: E/AndroidRuntime(12702):    at com.example.animalist.AnimalAdapter$1.onLoadingComplete(AnimalAdapter.java:123)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at com.nostra13.universalimageloader.core.DisplayBitmapTask.run(DisplayBitmapTask.java:74)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at android.os.Handler.handleCallback(Handler.java:587)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at android.os.Looper.loop(Looper.java:143)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at android.app.ActivityThread.main(ActivityThread.java:4263)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at java.lang.reflect.Method.invokeNative(Native Method)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at java.lang.reflect.Method.invoke(Method.java:507)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-30 17:01:53.713: E/AndroidRuntime(12702):    at dalvik.system.NativeStart.main(Native Method)
01-30 17:01:57.957: I/wpa_supplicant(15739): Reset vh_switch_counter due to receive LINKSPEED cmd

你有什么想法,这个问题有多解决?

提前致谢

1 个答案:

答案 0 :(得分:0)

如果Universal Image Loader允许在图像无法加载时设置图像,为什么需要监听器?

查看DisplayImageOptions。

options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_launcher) // HERE
                .showImageOnFail(R.drawable.ic_launcher) // HERE
                .bitmapConfig(Bitmap.Config.RGB_565).cacheOnDisc(true)
                .delayBeforeLoading(0).cacheInMemory(true) // HERE
                .resetViewBeforeLoading(false)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2).build();

在onGetView方法中,实现以下代码:

imageLoader.displayImage(imageUri, viewHolder.imgFoto, options);

ImageLoader是:

protected static ImageLoader imageLoader = ImageLoader.getInstance(); // Add this in your Activity or a BaseActivity that extends Activity.