开发者社区> 问答> 正文

图像视图返回Null指针异常

此处出现空指针异常我有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));

        }
    }
}

展开
收起
垚tutu 2019-12-19 16:22:31 605 0
1 条回答
写回答
取消 提交回答
  • 显然,null并不能作为基本数据类型的值,int a = null;这句代码编译都过不了,如果返回值为null的话就会尝试将null强转为基本数据类型,此时就会报空指针异常。但是对于其包装类型就不存在这个问题了,给包装类型返回null是合理的,因为Integer a = null是被允许的。

    2021-02-20 00:56:35
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
用计算和数据去改变整个世界 立即下载
重新定义计算的边界 立即下载
低代码开发师(初级)实战教程 立即下载