Ozuf Ozuf - 3 months ago 35
Android Question

Why is is custom listener null?

I am trying to create an interface between my Recyclerview Adapter and an Activity, so that when an item in the adapter is clicked, the Activity will react accordingly but the listener is always null.


public interface UrlTagCatClickedListener {
public void onUrlTagCatClicked (String chemicalURL);


public class TagCatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context mContext;
private List<TagCatItem> mTagCatItems;
private int lastPosition = -1;

private UrlTagCatClickedListener mTagCatClickedListener;

public TagCatAdapter(List<TagCatItem> tagCatItems, Context context) {
this.mTagCatItems = tagCatItems;
this.mContext = context;
this.mTagCatClickedListener = null;

public void setTagCatClickedListener (UrlTagCatClickedListener tagCatClickedListener) {
this.mTagCatClickedListener = tagCatClickedListener;

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tag_cat_item, parent, false);
return new ItemViewHolder(v);

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final TagCatItem tagCatItem = mTagCatItems.get(position);
((ItemViewHolder) holder).chemicalTitle.setText(Html.fromHtml(tagCatItem.getChemicalTitle()));
((ItemViewHolder) holder).chemicalDate.setText(tagCatItem.getChemicalDate());
((ItemViewHolder) holder).chemicalTitle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mTagCatClickedListener != null) { // mTagCatClickedListener is always null

public class ItemViewHolder extends RecyclerView.ViewHolder{
public TextView chemicalTitle, chemicalDate;
public ItemViewHolder(final View mView) {
chemicalTitle = (TextView) mView.findViewById(R.id.tagcat_title);
chemicalDate = (TextView) mView.findViewById(R.id.tagcat_date);

public int getItemCount() {
return mTagCatItems.size();


protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate called")
//Skipped a lot of codes that doesn't relate to the queation

List<TagCatItem> mTagCatItems = new ArrayList<>();

TagCatAdapter tagCatAdapter = new TagCatAdapter(mTagCatItems, this);
tagCatAdapter.setTagCatClickedListener(new UrlTagCatClickedListener() {
public void onUrlTagCatClicked(String chemicalURL) {
Log.d(TAG, "I go the url!!!!!!!!!! and it is + " + chemicalURL);

Please, do you have an idea why it's null and how I can fix it?


You have two different instances of TagCatAdapter in your given setup; one in the Activity, and one in the Fragment. You're setting the UrlTagCatClickedListener you've posted on the one created in the Activity, but it's the Fragment's TagCatAdapter instance that you're setting on the RecyclerView. That one doesn't have the listener set, so its mTagCatClickedListener is always null.

You just need to move the given UrlTagCatClickedListener to the TagCatAdapter in the Fragment, and remove the TagCatAdapter from the Activity, as you're not using that one anyway.