Search code examples
javaandroidviewmodel

Using observer inside dialogfragment causes program crash after the dialog window opens a second time


I recently asked this question but it got a bit messy so I thought i comprise all information a little bit better and try again.

I want to share data between a regular fragment and a dialog fragment to achieve this I use a shared view model. As you've seen from the title the software crashes the second time I open the dialog window.

Here is the main fragment that has a single button that when clicked opens up a dialog fragment.

public class MainFragment extends Fragment {

    private MainViewModel mViewModel;
    private View fragmentView;
    private SharedViewModel sharedViewModel;


    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.main_fragment, container, false);
        return fragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

        sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);

        Button aButton = fragmentView.findViewById(R.id.button);
        aButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedViewModel.setColor("Green");
                NavDirections action = MainFragmentDirections.actionMainFragmentToBlankFragment();
                Navigation.findNavController(getView()).navigate(action);

            }
        });

    }

}

Here is the code for the dialog fragment that pops up when i click the button.

public class BlankFragment extends DialogFragment {
    private View dialogView;
    private SharedViewModel sharedViewModel;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater layoutInflater = requireActivity().getLayoutInflater();
        dialogView = layoutInflater.inflate(R.layout.blank_fragment, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(dialogView);

        return builder.create();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        sharedViewModel = new ViewModelProvider(requireActivity()).
                get(SharedViewModel.class); //gets the shared view model from the associsated fragment.
        MutableLiveData<String> tableColor = sharedViewModel.getColor();
        tableColor.observe(getParentFragment().getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(getActivity(), "IF I GET HERE TWICE I DIE", Toast.LENGTH_SHORT).show();
            }

        });
    }
}

As you've probably seen i used a nav graph to navigate between them.

The shared view model looks like this

public class SharedViewModel extends ViewModel {
    private MutableLiveData<String> color = new MutableLiveData<>();


    public void setColor(String color){
        this.color.setValue(color);
    }
    public MutableLiveData<String> getColor(){
        return color;
    }

}

The error message reads:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.dialogfragmentcrashing, PID: 32620 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.widget.Toast.(Toast.java:121) at android.widget.Toast.makeText(Toast.java:286) at android.widget.Toast.makeText(Toast.java:276) at com.example.dialogfragmentcrashing.BlankFragment$1.onChanged(BlankFragment.java:47) at com.example.dialogfragmentcrashing.BlankFragment$1.onChanged(BlankFragment.java:44) at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131) at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149) at androidx.lifecycle.LiveData.setValue(LiveData.java:307) at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50) at com.example.dialogfragmentcrashing.SharedViewModel.setColor(SharedViewModel.java:11) at com.example.dialogfragmentcrashing.MainFragment$1.onClick(MainFragment.java:49) at android.view.View.performClick(View.java:7155) at android.view.View.performClickInternal(View.java:7124) at android.view.View.access$3500(View.java:808) at android.view.View$PerformClick.run(View.java:27370) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:359) at android.app.ActivityThread.main(ActivityThread.java:7418) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)


Solution

  • getParentFragment().getViewLifecycleOwner() is always the wrong LifecycleOwner to use as it is not destroyed when the DialogFragment is destroyed. Move all of that code to onCreate() and use this:

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedViewModel = new ViewModelProvider(requireActivity()).
                get(SharedViewModel.class); //gets the shared view model from the associsated fragment.
        MutableLiveData<String> tableColor = sharedViewModel.getColor();
        tableColor.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(getActivity(), "IF I GET HERE TWICE I DIE", Toast.LENGTH_SHORT).show();
            }
    
        });
    }