Fragment ClickListener error in RecyclerView

recyclerview onclicklistener in fragment
recyclerview item click listener in activity
recyclerview onclicklistener to new activity
recyclerview item click listener kotlin
recyclerview onclick position
recyclerview onclicklistener not working
recyclerview item click in fragment
how to open a different activity on recyclerview item onclick

I have converted a RecyclerView from Activity to Fragment all work but when i click on item i have this error:

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

This error is in myClickListener.onItemClick(getAdapterPosition(), v);

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter<MyRecyclerViewAdapter
        .DataObjectHolder> {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList<DataObject> mDataset;
    private static MyClickListener myClickListener;

    public static class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            myClickListener.onItemClick(getAdapterPosition(), v); //ERROR HERE
        }
    }

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
        mDataset = myDataset;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view_row, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());
    }

    public void addItem(DataObject dataObj, int index) {
        mDataset.add(index, dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}

This code is in fragment:

mView = inflater.inflate(R.layout.fragment_two, container, false);
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(false);
        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);
        return mView;

You are not calling

public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

from anywhere.

Call this method from your adapter or fragment. Or if you do not want right now, just add null check

@Override
public void onClick(View v) {
  if (myClickListener!= null) {
     myClickListener.onItemClick(getAdapterPosition(), v); //ERROR HERE
  }
}

And add below code into Fragment class.

   ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter.MyClickListener() {
        @Override
        public void onItemClick(int position, View v) {
            // Implement your logic to do after clicking on any item.
        }
    });

Set a click listener to a RecyclerView, If you've used a RecyclerView, you may know that they don't have a `​setOnItemClickListener` as OnClickListener() { @Override public void onClick(​View v) { listener. i want here on click method,i tried but it is not working hai Antonio Leiva i want to how to create a on item click listener in fragment..please tell me.. Reply. When i want to click recycler view then it should open another Activity Data is fetching but the problem is when i click on recycleview it displays nothing. I tried RecyclerView.OnItemTouchListene

Before calling the set adapter on your recyclerview call setOnItemClickListener

RecyclerView item Click Listener in Fragment with Firebase , In this video tutorial you will learn about firebase recyclerview adapter in fragment using Duration: 7:45 Posted: Sep 22, 2018 Hello I am having a hard time to implement this,I havent found any example to do it.I have gone up to this point myself but I am stuck!Any help would be appreciated public class JobsFragment extends Fragment{ private List jobsLists; private RecyclerView recycle; private FirebaseFirestore fb; private

I have solved with:

((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter
                .MyClickListener() {
            @Override
            public void onItemClick(int position, View v) {              
                Log.wtf("TEST", position);
            }
        });

Fragment with RecyclerView Part 3 : item Click Listener Event , Hello guys and welcome back to my channel, in this video i will show you how to add a click Duration: 25:07 Posted: Feb 21, 2018 Change this to getActivity(), because you are using recyclerView in Fragment. this is the context of Activity. getActivity() is the context of Fragment. Code : RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 3); to . RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3);

Android Kotlin Fundamentals 07.4: Interacting with RecyclerView items, Building a basic user interface using an activity, fragments, and views. How to implement a basic RecyclerView with an Adapter , ViewHolder , and item layout. Implement a click listener to navigate to a detail view when an item is clicked. You will get a compiler error because you added a parameter to the function call​  Set a click listener to a RecyclerView. If you’ve used a RecyclerView, you may know that they don’t have a setOnItemClickListener as ListView had, so we have to create our own way to do it. Many people asked me how to do this, so that’s why I decided to write about it. There are many ways to achieve this, but I will show you the way I usually do it.

Click listener for RecyclerView adapter, Lastly, here is an example of a fragment where I create this listener. I am simply toasting the position that was tapped on, but you could do  In this video I show you how to add an OnClickListener to your recyclerview and display data in a new activity. In the new activity you'll learn to display an image using an ImageView widgets and

Android RecyclerView onItemClickListener & getAdapterPosition , Android RecyclerView onItemClickListener & getAdapterPosition(): A Better Way many other things) from view.getTag() i.e. ViewHolder's object inside your Activity/Fragment/View. OnClickListener and initialize it in the way we used to do. A simple Fragment with RecyclerView. GitHub Gist: instantly share code, notes, and snippets.

Comments
  • @Marcus you have called itemView.setOnClickListener(this); instead.
  • Sorry im very new in android i can't understand your answer
  • @MarcusJ.Kennedy See my last update. Add this code before doing "return mView;". And I will strongly suggest that before doing such things you need to read basics. This is not a place to provide code and why a code is not working. So please take care in future
  • Thanks but i have an error: "Cannot resolve method setOnItemClickListener"