Nullpointer Exception on getActivity().finish()

  android, nullpointerexception

My question is simple. I do have nullcheck for context and activity but when i’ve checked on crashlytics there is a crash for this method. How can i prevent it ?

 public void continueWithDashboard() {
        if (getActivity() == null || getContext() == null) {
            Log.e(TAG, "continueWithDashboard: activity null");
            return;
        }

        getActivity().startActivity(new Intent(getActivity(), DashboardActivity.class));
        getActivity().finish();
    }

and there is crash

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'void android.app.Activity.finish()' on a null object reference
xx.continueWithDashboard (SplashFragment.java:106)
xx.splashCompletedFunction (SplashPresenter.java:298)
xx.SplashPresenter.access$1000 (SplashPresenter.java:45)
xx.SplashPresenter$5.run (SplashPresenter.java:281)
java.util.TimerThread.mainLoop (TimerThread.java:562)
java.util.TimerThread.run (TimerThread.java:512)

Source: Android Questions

LEAVE A COMMENT