0

I have a number of reports in Firebase Crashlytics for the following crash,

Fatal Exception: java.lang.IllegalStateException: View null does not have a NavController set
       at androidx.navigation.Navigation.findNavController(Navigation.java:84)
       at com.***.splash.SplashFragment.lambda$checkLoggedInStatus$1$SplashFragment(SplashFragment.java:xxx)
       at com.***.splash.-$$Lambda$SplashFragment$VafildstAQkrvymiO-AhxCqBHIE.run(lambda)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7325)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

This is my SplashFragment

public class SplashFragment extends DaggerFragment {

    ...

    private View view;

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

    @Override
    public void onResume() {
      super.onResume();
      checkLoggedInStatus();
    }

    private void checkLoggedInStatus() {
      if (!splashViewModel.isLoggedIn()) {
        new Handler().postDelayed(() - >
             Navigation
                 .findNavController(view) // line xxx
                 .navigate(R.id.action_splashFragment_to_loginFragment), 1500);
      } else {...}
    }

    ...

    @Override
    public void onDestroy() {
      view = null;
      super.onDestroy();
    }
  }

I don't get this issue in my emulators or even in real devices for both DEBUG and RELEASE build types. So I'm having a hard time tracking it down. This happens to around 1.75% live users. So my question is,

How does the view becomes null only for several users?

Can someone suggest a work around for this?

Roshana Pitigala
  • 7,058
  • 8
  • 38
  • 66
  • 1
    view is available only after `onCreateView()`. You're accessing it to early. Why don't you move your logic inside `onCreatedView()` ? – M D Jul 21 '20 at 06:40
  • @MD `onResume()` is called way after `onCreateView()`. So by that time the view is already created. Isn't it? – Roshana Pitigala Jul 21 '20 at 06:42
  • Yes you're right but based on your crash it's seems wrong. – M D Jul 21 '20 at 06:44
  • Check [this](https://stackoverflow.com/questions/50502269/illegalstateexception-link-does-not-have-a-navcontroller-set) – M D Jul 21 '20 at 06:51
  • @MD I saw this post before and yes I have a `` with `android:name="androidx.navigation.fragment.NavHostFragment"` attribute in my `activity_main.xml` – Roshana Pitigala Jul 21 '20 at 06:56
  • Note that I'm not getting this at all when I'm developing or testing, it's only in production. – Roshana Pitigala Jul 21 '20 at 06:58
  • 1
    I guess this is happening for slower devices. They are taking a lot of time to render the view and hence its is becoming null. You should add a null check before changing the navigation controller. – Pronoy999 Jul 21 '20 at 07:38

0 Answers0