1

Thanks for taking into account my publication, I could implement a menu with elements in the view of a recycler, but with my problem is to access the click event of the menu items, I have seen that they do it from activities, but I have searched for information to do it in recycler adapters and I can not find anything. I must add that I have it implemented but even so I get an error I hope you can tell me what I do wrong or the correct way to implement it.

public class ViewHolderPedido extends RecyclerView.ViewHolder implements View.OnClickListener, PopupMenu.OnMenuItemClickListener {

        @BindView(R.id.txtNumeroPedido)
        TextView txtNumeroPedido;
        @BindView(R.id.txtFechaPedido)
        TextView txtFechaPedido;
        @BindView(R.id.txtSubTotalPedido)
        TextView txtSubTotal;
        @BindView(R.id.txtEstadpoPedidoList)
        TextView txtEstadoPedido;
        @BindView(R.id.txtNombreProductoPedidoList)
        TextView txtNombreProduct;
        @BindView(R.id.imgProductoPedidoList)
        ImageView imgProducto;
        @BindView(R.id.menu)
        ImageView mImgMenu;
        @BindView(R.id.content_order)
        LinearLayout mContentOrder;

        PopupMenu popupMenu;
        PedidoModelo pedidoModelo;

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.menu:
                    popupMenu = new PopupMenu(mContext, view);
                    popupMenu.getMenuInflater().inflate(R.menu.menu_cancel, popupMenu.getMenu());
                    popupMenu.show();
                    break;
                case R.id.content_order:
                    mListener.OnItemClick(view, pedidoModelo, getAdapterPosition(), true);
                    break;
            }
        }

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return false;
        }

        public ViewHolderPedido(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mImgMenu.setOnClickListener(this);
            mContentOrder.setOnClickListener(this);
            popupMenu.setOnMenuItemClickListener(this);
        }

        protected void bind(PedidoModelo model) {
            this.pedidoModelo = model;

            txtFechaPedido.setText(pedidoModelo.getFecha());
            txtSubTotal.setText(PrecioFormater.formatoPrecio(pedidoModelo.getPrecioTotal()));
            txtNumeroPedido.setText(pedidoModelo.getNumeroOrden());
            txtEstadoPedido.setText(pedidoModelo.getEstado());
            txtNombreProduct.setText(pedidoModelo.getNombreProducto());
            Picasso.with(mContext).load(pedidoModelo.getUrlImagen()).into(imgProducto);
            switch (pedidoModelo.getCodigo_estado()) {
                case OrderStatus.PENDING:
                    mImgMenu.setVisibility(View.VISIBLE);
                    break;
                case OrderStatus.COMPLETE:
                    mImgMenu.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    public interface OnItemClickListener {

        boolean OnItemClick(View view, PedidoModelo item, int position, boolean longPress);

    }
}

This error I get when loading my recycler

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.PopupMenu.setOnMenuItemClickListener(android.widget.PopupMenu$OnMenuItemClickListener)' on a null object reference
dbenitobaldeon
  • 324
  • 3
  • 20
  • Despite the fact that in your program text assignment of `popupMenu` field occurs before the class constructor, in actual fact, constructor is always being called first, when your `popupMenu` is still an unassigned `null`. Method `onClick` will be called after something happens, and might not be called at all. – M. Prokhorov Dec 20 '18 at 18:36
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – M. Prokhorov Dec 20 '18 at 18:36
  • @M.Prokhorov that has nothing to do with my code is only ordered by life cycle and according to the java and android documentation the override methods go first and then in the same order the methods go. I am interested in the problem that I present. – dbenitobaldeon Dec 20 '18 at 18:42
  • I already told you what happens. Please read linked question, and initialise your field values. – M. Prokhorov Dec 20 '18 at 19:43

1 Answers1

1

I found my solution, it happened that I was declaring the setOnMenuItemClickListener method in the constructor, which was the right thing to do when instantiating it when I loaded my menu. Here I leave the solution.

@Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.menu:
                    popupMenu = new PopupMenu(mContext, view);
                    popupMenu.getMenuInflater().inflate(R.menu.menu_cancel, popupMenu.getMenu());
                    popupMenu.setOnMenuItemClickListener(this);
                    popupMenu.show();
                    break;
                case R.id.content_order:
                    mListener.OnItemClick(view, pedidoModelo, getAdapterPosition(), true);
                    break;
            }
        }

como se ve el metodo ya esta declarado correctamente. me ayudo el post que me pasaron sobre como fixear los null pointer

What is a NullPointerException, and how do I fix it?

dbenitobaldeon
  • 324
  • 3
  • 20