gpt4 book ai didi

java - android studio中的类适配器

转载 作者:行者123 更新时间:2023-11-29 02:41:11 25 4
gpt4 key购买 nike

我有包含 ImageView 和 TextView 的类适配器,我从执行后接收到数组列表,然后将这个数组传递给我的适配器,当运行我的应用程序时,它给我这个错误。

 java.lang.NullPointerException: Attempt to read from field 'android.widget.ImageView com.gmplatform.gmp.MainActivity_navagation_custmoer$itemAdapter3$ViewHolder.ivpic1' on a null object reference
at com.gmplatform.gmp.MainActivity_navagation_custmoer$itemAdapter3$ViewHolder.access$2600(MainActivity_navagation_custmoer.java:2410)
at com.gmplatform.gmp.MainActivity_navagation_custmoer$itemAdapter3.getView(MainActivity_navagation_custmoer.java:2372)

下面是适配器类

public class itemAdapter3 extends  BaseAdapter{

private ArrayList<favouriteitems> itemCatalog;
private int resource;
private LayoutInflater inflater;

public itemAdapter3(Context context, int resource, List<favouriteitems> objects) {
// super(context, resource, objects);
itemCatalog = (ArrayList<favouriteitems>) objects;
this.resource = resource;
inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
}



public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return itemCatalog.size();
}

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

@Override
public long getItemId(int position) {
return itemCatalog.indexOf(getItem(position));
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final int pos = position;
final favouriteitems items = itemCatalog.get(pos);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.ivpic1 = (ImageView) convertView.findViewById(R.id.iv);
holder.st = (ImageView)convertView.findViewById(R.id.st);
holder.sh=(ImageView)convertView.findViewById(R.id.sh);
holder.name = (TextView) convertView.findViewById(R.id.t4);
holder.pr = (TextView) convertView.findViewById(R.id.textView32);
holder.av = (TextView) convertView.findViewById(R.id.av);
holder.t5 = (TextView) convertView.findViewById(R.id.t5);
final ViewHolder finalHolder = holder;



}
else {
holder = (MainActivity_navagation_custmoer.itemAdapter3.ViewHolder) convertView.getTag();
}

final ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);

// Then later, when you want to display image
ImageLoader.getInstance().displayImage(itemCatalog.get(position).getM1().getLoc2().getItmc().getIt().getPic(), holder.ivpic1 , new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}

@Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}

});


holder.pr.setText(String.valueOf(itemCatalog.get(position).getM1().getLoc2().getItmc().getPrice() + " SR"));
holder.name.setText(itemCatalog.get(position).getM1().getLoc2().getItmc().getIt().getName());

if (itemCatalog.get(position).getM1().getLoc2().getItmc().isInSc() == true) {
holder.sh.setImageResource(R.drawable.customern2);
} else {
holder.sh.setImageResource(R.drawable.customern1);
}
holder.t5.setText(itemCatalog.get(position).getM1().getmName()+" ("+itemCatalog.get(position).getM1().getLoc2().getArea()+" )");

return convertView;
}

class ViewHolder {
private ImageView ivpic1,sh,st;
private TextView name,pr,t5,av;

}
}

最佳答案

Constructor 有问题:

取消 super 调用的注释并运行它,您将不再出现空指针异常。

public itemAdapter3(Context context, int resource, List<favouriteitems> objects) {
super(context, resource, objects);
itemCatalog = (ArrayList<favouriteitems>) objects;
this.resource = resource;
inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
}

关于java - android studio中的类适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002243/

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