Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick fragment switching view binding null

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
like image 345
Abhishek AN Avatar asked Apr 10 '26 16:04

Abhishek AN


1 Answers

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

like image 164
Azhar Ali Avatar answered Apr 13 '26 07:04

Azhar Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!