Skip to content
Advertisement

Null pointer Exception on Recycler View On item click listener Activity Close when item got clicked

Hello I am new in Android development
I have tried different way to show data in RecyclerView from firebase. At first I set on click listener on item of RecyclerView and it work’s fine but After that I make some changes in code now I am trying to test it’s showing Null Exception pointer but I didn’t change anything on Adapter So why it’s showing Null pointer Exception can you please check Where I am mistaking. When I click on RecyclerView item Activity is automatically getting close.

see my Log cat

*LogCat

2020-10-29 16:15:14.953 21026-21026/? E/gamesdownloade: Unknown bits set in runtime_flags: 0x8000
2020-10-29 16:15:14.977 21026-21026/? E/libc: Access denied finding property "persist.sys.theme"
2020-10-29 16:15:15.041 21026-21055/? E/Perf: Fail to get file list com.downloadfrom.psp.gamesdownloader
2020-10-29 16:15:15.042 21026-21055/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-10-29 16:15:15.042 21026-21055/? E/Perf: Fail to get file list com.downloadfrom.psp.gamesdownloader
2020-10-29 16:15:15.042 21026-21055/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-10-29 16:15:24.376 21026-21026/com.downloadfrom.psp.gamesdownloader E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.downloadfrom.psp.gamesdownloader, PID: 21026
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.downloadfrom.psp.gamesdownloader.ViewHolder$ClickListener.onItemClick(android.view.View, int)' on a null object reference
        at com.downloadfrom.psp.gamesdownloader.ViewHolder$1.onClick(ViewHolder.java:23)
        at android.view.View.performClick(View.java:7259)
        at android.view.View.performClickInternal(View.java:7221)
        at android.view.View.access$3800(View.java:821)
        at android.view.View$PerformClick.run(View.java:27716)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:227)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

This is my View Holder/Adepter of Recycler view Check it please.

package com.example.my.app;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

public class ViewHolder extends RecyclerView.ViewHolder {

    View view;

    public ViewHolder( View itemView) {
        super(itemView);

        view = itemView;

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mClickListener.onItemClick(view,getAdapterPosition());
            }
        });

        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                mClickListener.onItemLongClick(view, getAdapterPosition());

                return true;
            }
        });
    }

    public void setDetails(Context ctx, String title, String image,String download,String size, String discription){

        TextView GameName;
        TextView Size,Download,Discription;
        ImageView GameImage;

        Size = view.findViewById(R.id.Getsize);
        Download = view.findViewById(R.id.Getdownload);
        Discription = view.findViewById(R.id.Getdiscription);

        GameName = view.findViewById(R.id.title_item);
        GameImage = view.findViewById(R.id.thumbnail_item);

        GameName.setText(title);
        Picasso.get().load(image).into(GameImage);
        Size.setText(size);
        Download.setText(download);
        Discription.setText(discription);


    }

    private ViewHolder.ClickListener mClickListener;

    public interface ClickListener{

        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }

    public void setOnClickListener(ViewHolder.ClickListener clickListener){

        mClickListener = clickListener;

    }

}

This is my MainActivity On start method In the this method I am showing data in Recycler View from firebase And Also set on click listener here check it please

//MainActivity On start Method

@Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Games, ViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<Games, ViewHolder>(
                        Games.class,
                        R.layout.cardview_list_item,
                        ViewHolder.class,
                        mRef
                ) {
                    @Override
                    protected void populateViewHolder(ViewHolder viewHolder, Games games, int i) {

                        viewHolder.setDetails(getApplicationContext(), games.getTitle(), games.getImage(),
                                games.getSize(),games.getDownload(),games.getDiscription());
                    }

                    @Override
                    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                        ViewHolder viewHolder = super.onCreateViewHolder(parent,viewType);

                        viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {

                                Toast.makeText(MainActivity.this,"You Clicked on this Game",Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void onItemLongClick(View view, int position) {
                                Toast.makeText(MainActivity.this,"You Long Clicked on this Game",Toast.LENGTH_SHORT).show();

                            }
                        });

                        return super.onCreateViewHolder(parent, viewType);
                    }
                };


        recyclerView.setAdapter(firebaseRecyclerAdapter);


    }

And Also check Model class

    package com.example.my.app;

public class Games {

    String title;
    String size;
    String image;
    String download;
    String discription;

    public Games() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getDownload() {
        return download;
    }

    public void setDownload(String download) {
        this.download = download;
    }

    public String getDiscription() {
        return discription;
    }

    public void setDiscription(String discription) {
        this.discription = discription;
    }
}

Advertisement

Answer

Your ViewHolder is null inside the onCreateViewHolder method

Instead of using

ViewHolder viewHolder = super.onCreateViewHolder(parent,viewType);

Use this

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_xml_layout, parent, false);
return new ViewHolder(view);

and @Override onBindViewHolder method

@Override
protected void onBindViewHolder(ViewHolder holder, final int position, Model model) {
    holder.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              // your click logic
          }
     });
}

for ref check this link

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement