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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With