11

I have 2 fragments in a tab layout, switching between them quickly causes the views to be returned as null, using ViewBinding. Is this because of the delay to build the FragmentXBinding class?

Usage Example:

chatadapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                binding.chatRecyclerView.smoothScrollToPosition(0);
            }
        });

Error:

java.lang.NullPointerException: Attempt to read from field 'androidx.recyclerview.widget.RecyclerView com.iku.databinding.FragmentChatBinding.chatRecyclerView' on a null object reference
Abhishek AN
  • 648
  • 7
  • 24

1 Answers1

0

FragmentX

 FragmentXBinding binding;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    binding = FragmentChatBinding.inflate(inflater, container, false);
    init();
    return binding.getRoot();
}

 private void init() {
    // all initialization 
}

This helps me to solve my error with null reference

Azhar Ali
  • 1,961
  • 3
  • 13
  • 24