0

this is not a duplicate question as the occurrence of the issue is different.. as i am only experiencing this behavior when i try to do exact what i have mentioned(layout order wise)

i am trying to create a new dialog fragment trough another dialog fragment. when i try to do that it throws an error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
        at android.support.v4.app.DialogFragment.show(DialogFragment.java:142)

this is the related code

ForgotPasswordFailedFragment forgotPasswordFailedFragment = new ForgotPasswordFailedFragment();
        forgotPasswordFailedFragment.show(getFragmentManager(), "Password Reset Failed");

so my view order is something like this..

mainActivity -> fragment -> dialogfragment -> dialogfragment (i'am trying to get this)

i have tried mentioned answers and tease are the errors that occurred for each change...

ForgotPswrdSuccessFragment forgotPswrdSuccessFragment = new ForgotPswrdSuccessFragment(); forgotPswrdSuccessFragment.show(requireActivity().getSupportFragmentManager(), "Password Reset Failed");

this throws

 Fragment ForgotPasswordDFragment{725811b} not attached to an activity.
            at android.support.v4.app.Fragment.requireActivity(Fragment.java:724)
            at *********.SignIn.ForgotPassword.ForgotPasswordDFragment.showSuccessDialog(ForgotPasswordDFragment.java:85)

ForgotPasswordDFragment is a dialog fragment which is on a fragment and i am calling another dialogfragment from (ForgotPasswordDFragment) there..and now that action is throwing above mentioned eror

for this

forgotPswrdSuccessFragment.show(this.getChildFragmentManager(), "Password Reset Failed");

the exception is

java.lang.IllegalStateException: Fragment has not been attached yet. at android.support.v4.app.Fragment.instantiateChildFragmentManager(Fragment.java:2383)

RAINA
  • 802
  • 11
  • 22
  • Try replacing getFragmentManager() to getSupportFragmentManager(); – Sabyasachi Dec 06 '18 at 12:45
  • I think your DialogFragment is a type of support.v4.app and for this you should be using getSupportFragmentManager() instead of getFragmentManager(). – Surender Kumar Dec 06 '18 at 12:46
  • thanks for replying ..i cannot access getSupportFragmentManager().. i entered my layout order into the question .. – RAINA Dec 06 '18 at 12:50

3 Answers3

0

Hi @Sha this might be helpful:

YourDialog yourDialog = YourDialog.newInstance(); 
yourDialog.show(getSupportFragmentManager(),"DialogFragment"); 
procra
  • 535
  • 4
  • 29
satyan_android
  • 364
  • 4
  • 15
0

FragmentManager will be null until it is attached to the activity.

So use below code ,

If it is a nested Fragment use this.getChildFragmentManager() for your fragment class

else use getActivity().getFragmentManager() or getActivity().getSupportFragmentManager().

0

getFragmentManager() might return null, if the fragment is not attached to a context. This could result in uncaught exceptions by passing a null reference, as the stack trace already mentioned. The most direct way would be to wrap getFragmentManager() into a not-null-condition

FragmentManager manager = getFragmentManager();

if (manager != null) {
    //your code
}

However, to avoid blind null passengers in your code, consider using getSupportFragmentManager(). This method is not directly callable on a fragment. So you have to call getActivity().getSupportFragmentManager() or requireActivity().getSupportFragmentManager() respectively.

procra
  • 535
  • 4
  • 29
  • thanks for the reply..i have update my code to according to your suggestion of checking null..thanks for that..i have tried other answers but no luck..i have updated my question – RAINA Dec 07 '18 at 04:19
  • to show a fragment via `FragmentSupportManager` call `getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new forgotPswrdSuccessFragment()).commit();` you actually don't need to instantiate the new fragment instance, despite it's a good way to do so. And don't forget to wrap this in a not-null clause also, `getActivity` might return null. – procra Dec 10 '18 at 08:41
  • for more detail, `getActivity()` and `requireActivity()` do almost the same thing. The difference is that `requireActivity()` throws an exception while `getActivity()` just returns null – procra Dec 10 '18 at 08:45
  • ..but my issue still remains .. i have updated the question ..can you please give a look.. – RAINA Dec 10 '18 at 09:33