此处出现空指针异常我有2个recyclerviews和2个适配器。一种用于商店名称,一种用于每个商店的可交付成果。我需要显示每个商店的可交付成果。当我填充布局组件时,可交付的imageview返回null。
这是我的代码,用于填充可交付成果的recyclerview。
public class DeliverableView extends ConstraintLayout {
@BindView(R.id.iv_deliverable_image)
ImageView iv_deliverable_image;
@BindView(R.id.tv_deliverable_name)
TextView tv_deliverable_name;
@BindView(R.id.tv_deliverable_size)
TextView tv_deliverable_size;
@BindView(R.id.tv_deliverable_quantity)
TextView tv_deliverable_quantity;
@BindView(R.id.tv_deliverable_amount)
TextView tv_deliverable_amount;
public DeliverableView(Context context) {
super(context);
}
public DeliverableView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DeliverableView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void populate(Entity deliverable) {
Glide.with(getContext()).load(StringUtil.trimUrlByHeight(deliverable.attributes.image_url, 60)).into(iv_deliverable_image);
tv_deliverable_quantity.setText(String.valueOf(deliverable.attributes.count));
tv_deliverable_name.setText(deliverable.attributes.name);
tv_deliverable_size.setText(deliverable.attributes.size);
tv_deliverable_amount.setText(StringUtil.getDisplayedBill(deliverable.attributes.amount_in_cents * deliverable.attributes.count));
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View.inflate(getContext(), R.layout.layout_componet_deliverable, this);
ButterKnife.bind(this);
}
}
Below is the code for the adapter of shops. This is where I setup the deliverableadapter and updating it.
public class ViewReceiptShopNameAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<Entity> jobOrders = new ArrayList<>();
private DeliverableAdapter deliverableAdapter;
private List<OrderItem> orderItems = new ArrayList<>();v
public ViewReceiptShopNameAdapter(Context context) {
this.context = context;
this.inflater = LayoutInflater.from(context);
}
public void updateViewShopAdapter(List<Entity> jobOrders) {
this.jobOrders = jobOrders;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
return new ViewHolderShopName(inflater.inflate(R.layout.layout_view_receipt_shopname, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final Entity jobOrder = jobOrders.get(position);
// final OrderItem orderItemList = orderItems.get(position);
ViewHolderShopName viewHolder = (ViewHolderShopName) holder;
viewHolder.bindModel(jobOrder);
}
@Override
public int getItemCount() {
return jobOrders.size();
}
class ViewHolderShopName extends RecyclerView.ViewHolder {
@BindView(R.id.store_name)
TextView tv_shop_name;
@BindView(R.id.count_items_delivered)
TextView tv_items_count;
@BindView(R.id.rv_receipt_items)
RecyclerView rv_receipt_items;
ViewHolderShopName(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
void bindModel(Entity jobOrder) {
// tv_shop_name.setText(orderItem.shop.attributes.name);
tv_items_count.setText(String.format("%d items delivered", jobOrder.attributes.deliverables_purchased_count));
//deliverable adapter
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv_receipt_items.setLayoutManager(layoutManager);
deliverableAdapter = new DeliverableAdapter(context);
rv_receipt_items.setAdapter(deliverableAdapter);
deliverableAdapter.updateDeliverableAdapter(Arrays.asList(jobOrder.relationships.deliverables.data));
}
}
}
显然,null并不能作为基本数据类型的值,int a = null;这句代码编译都过不了,如果返回值为null的话就会尝试将null强转为基本数据类型,此时就会报空指针异常。但是对于其包装类型就不存在这个问题了,给包装类型返回null是合理的,因为Integer a = null是被允许的。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。