I'm facing a java.lang.NullPointerException: Attempt to read from field 'android.widget.ImageView com.cottdev.autoserv.databinding.FragmentArtistProfileBinding.ivEditPersonal' on a null object reference
problem after migrating to androidx. Kindly let me know how I can go about it.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_artist_profile, container, false);
prefrence = SharedPrefrence.getInstance(getActivity());
userDTO = prefrence.getParentUser(Consts.USER_DTO);
baseActivity.headerNameTV.setText(getResources().getString(R.string.my_profile));
parmsCategory.put(Consts.USER_ID, userDTO.getUser_id());
parms.put(Consts.ARTIST_ID, userDTO.getUser_id());
parms.put(Consts.USER_ID, userDTO.getUser_id());
setUiAction();
return binding.getRoot();
}
public void setUiAction() {
binding.ivEditPersonal.setOnClickListener(this);
binding.appbar.addOnOffsetChangedListener(this);
binding.btnDelete.setOnClickListener(this);
binding.btnChange.setOnClickListener(this);
mMaxScrollSize = binding.appbar.getTotalScrollRange();
binding.swOnOff.setOnClickListener(v -> {
if (artistDetailsDTO != null) {
if (NetworkManager.isConnectToInternet(getActivity())) {
paramsUpdate = new HashMap<>();
paramsUpdate.put(Consts.USER_ID, userDTO.getUser_id());
if (artistDetailsDTO.getIs_online().equalsIgnoreCase("1")) {
paramsUpdate.put(Consts.IS_ONLINE, "0");
isOnline();
} else {
paramsUpdate.put(Consts.IS_ONLINE, "1");
isOnline();
}
} else {
ProjectUtils.showToast(getActivity(), getResources().getString(R.string.internet_concation));
}
} else {
ProjectUtils.showToast(getActivity(), getResources().getString(R.string.incomplete_profile_msg));
}
});
I think the problem is your binding.
You can use a diffrenete way to overide onCreateView
First import import androidx.fragment.app.DialogFragment;
then extand extends DialogFragment
and you overide onCreateview
like this
@Nullable @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_artist_profile, container, false);
// you can call xml element by using view.findviewbyid ..
return view;
}