Home > Mobile >  RecyclerViewClickListener returns null
RecyclerViewClickListener returns null

Time:04-01

I have two activities and two layouts. When I display the list in the first activity, everything works and gives me the number of the item in the list when clicked, but when I try to repeat the same thing in the second activity, it gives me that the RecyclerViewClickListener listener is null.

Adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private ArrayList<String> array;
    private RecyclerViewClickListener listener;

    public MyAdapter( ArrayList<String> arrays, RecyclerViewClickListener listener)
    {
        this.listener = listener;
        this.array = arrays;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.textView.setText(array.get(position));
    }

    @Override
    public int getItemCount() {

        return array.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public TextView textView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            textView = itemView.findViewById(R.id.text_card);
            itemView.setOnClickListener(this);
            System.out.println(itemView);
        }

        @Override
        public void onClick(View v) {
            System.out.println(listener);
            System.out.println(v);
            System.out.println(getAdapterPosition());
            listener.onClick(v, getAdapterPosition());
        }
    }

    public interface  RecyclerViewClickListener{
        void onClick(View v, int position);
    }

}

first activity:

ArrayList<String> list_of_name= new ArrayList<>();
private RecyclerView recyclerView;
    private MyAdapter.RecyclerViewClickListener listener;

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity1);
        recyclerView = findViewById(R.id.recyclerView);
     
          list();
        
    }

 public void list() {

        MyAdapter myAdapter = new MyAdapter(list_of_name, listener);
        recyclerView.setAdapter(myAdapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        listener = (v, position) -> {
           
            itemPosition = list_of_name.get(position);
            Syste.out.println(itemPosition );
        };
    }

Second activity:

    private RecyclerView recyclerView;
    private MyAdapter.RecyclerViewClickListener listener;
    ArrayList<String> list2= new ArrayList<>();

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity2);

        recyclerView = findViewById(R.id.recyclerView1);

        File internalStorageDir = getFilesDir();
        File alice = new File(internalStorageDir, "file/");

        for (File file : alice.listFiles()) {
            list2.add(0, file.getName());
        }

        view();
    }

    public void view()
    {
        MyAdapter adapter = new MyAdapter(list2, listener);
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        listener = (v, position) -> {
            itemPosition = list2.get(position);
            Syste.out.println(itemPosition );
        };
    }

Error:

I/System.out: null
    android.widget.LinearLayout{ecdf5a1 VFE...C.. ...P.... 0,0-1080,167}
    0
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 28268
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.myapplication.recycler.MyAdapter$RecyclerViewClickListener.onClick(android.view.View, int)' on a null object reference
        at com.example.myapplication.recycler.MyAdapter$MyViewHolder.onClick(MyAdapter.java:59)
        at android.view.View.performClick(View.java:7185)
        at android.view.View.performClickInternal(View.java:7162)
        at android.view.View.access$3500(View.java:819)
        at android.view.View$PerformClick.run(View.java:27684)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/MQSEventManagerDelegate: failed to get MQSService.

I can’t understand why in the first case it processes the click normally, and in the second it says that the RecyclerViewClickListener is null

CodePudding user response:

public void list() {

        MyAdapter myAdapter = new MyAdapter(list_of_name, listener);
        recyclerView.setAdapter(myAdapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        listener = (v, position) -> {
           
            itemPosition = list_of_name.get(position);
            System.out.println(itemPosition );
        };
    }

This is where the issue is coming. You are passing the listener first and then initialising it later. This is causing it to be null. Why don't you try this?:

public void list() {

        listener = (v, position) -> {

            itemPosition = list_of_name.get(position);
            System.out.println(itemPosition );
        };

        MyAdapter myAdapter = new MyAdapter(list_of_name, listener);
        recyclerView.setAdapter(myAdapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this);
    }
  • Related