简体   繁体   中英

How do I fix android app crashing after a java.lang.NullPointerException

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

  • at com.cottdev.autoserv.ui.fragment.ArtistProfile.setUiAction
  • at com.cottdev.autoserv.ui.fragment.ArtistProfile.onCreateView)

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;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM