gpt4 book ai didi

java - ListView 适配器中的 ViewHolder 不起作用

转载 作者:行者123 更新时间:2023-12-01 18:37:12 25 4
gpt4 key购买 nike

我一直在尝试使用 ViewPager 让 ListView 滚动更平滑,但它总是崩溃。我尝试了大约 3 种解决方案。屏幕打开,显示加载指示器,但过了一会儿(大约 3-5 秒)它崩溃了。要从 URL 加载图像,我使用 Universal-Image-Loader lib由诺斯特拉13。无需添加 ViewHolder,一切都运行良好。

Adapter 类中我的 LogCat 和 getView + ViewHolder:

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

在AnimalAdapter.java中 - 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 = null;
@Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub

}

@Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub

}

@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;
}

你有什么想法,如何改进它吗?

提前致谢

最佳答案

事实上你在堆栈跟踪中

01-30 16:45:09.003: E/AndroidRuntime(12533):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
01-30 16:45:09.003: E/AndroidRuntime(12533): at android.widget.ListView.onMeasure(ListView.java:1127)

表明您返回的convertView为null

关于第二个问题,ImageLoadingListener 内的 ViewHolder 实例始终为 null。您应该注意相应地初始化它

关于java - ListView 适配器中的 ViewHolder 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21461247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com