Click RecyclerView - Item


I need to click on an item in a RecyclerView but when I try to do it it shows me the following message:

java.lang.NullPointerException: Attempt to invoke interface method 'void (android.view.View, int, boolean)' on a null object reference

What I am doing is the following:

I created an interface in the following way:

   public interface ItemClickListener {
    void onClick(View view,int position,boolean isLongClick);

After that I created a Java class for myself

public class MenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView txtNombre;
    public ImageView imgWallpaper;
    private ItemClickListener itemClickListener;

    public MenuViewHolder(@NonNull View itemView) {
        txtNombre = (TextView)itemView.findViewById(;
        imgWallpaper = (ImageView)itemView.findViewById(;


     public void setItemClickListener(ItemClickListener itemClickListener) {
         this.itemClickListener = itemClickListener;

    public void onClick(View view) {


And finally in my main, I connect to a database in firebase and I show some images in the following way:

// Declaration of global variables

 FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference category = database.getReference("Wallpaper");

    RecyclerView recycler_menu;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<wallpaperClass, MenuViewHolder> adapter;

// Main code

FirebaseRecyclerOptions <wallpaperClass> options =
                new FirebaseRecyclerOptions.Builder<wallpaperClass>()

        adapter = new FirebaseRecyclerAdapter<wallpaperClass, MenuViewHolder>(options) {
            protected void onBindViewHolder(@NonNull MenuViewHolder holder, int position, @NonNull wallpaperClass model) {

            public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item,parent,false);
                return new MenuViewHolder(v);


Everything works very well, the images are shown as they correspond but when I click on an image it takes me out of the app.

I thank all of you already for your help.

greetings ..

Solution: Add this code to onBindViewHolder, validate the click from that area and you're done. thank you all.

protected void onBindViewHolder(@NonNull MenuViewHolder holder, int position, @NonNull final wallpaperClass model) {

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        Toast.makeText(getApplicationContext(), "Link: " + model.getLinkwall().toString(), Toast.LENGTH_SHORT).show();
                        /*Intent Wallpaper = new Intent(getApplicationContext(),homeActivity.class);

asked by Jeffrey Concua 14.10.2018 в 06:10

0 answers