title: Automatic logout of Android App
body: <p>I know there are several answers to this question, but it isn't working for me, currently my code is:</p>
  
  <pre><code>public class LogoutService extends Service {
  
      public static CountDownTimer timer;
      @Override
      public void onCreate(){
          // TODO Auto-generated method stub
          super.onCreate();
          timer = new CountDownTimer(1 * 60 * 1000, 1000) {
              public void onTick(long millisUntilFinished) {
                  //Some code
                  Log.v(TAG, "Service Started");
              }
  
              public void onFinish() {
                  Log.v(TAG, "Call Logout by Service");
                  // Code for Logout
                  stopSelf();
              }
          };
      }
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  }
  </code></pre>
  
  <p>and in every activity I have:</p>
  
  <pre><code>@Override
  protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
  
      LogoutService.timer.start();
  }
  
  @Override
  protected void onStop() {
      // TODO Auto-generated method stub
      super.onStop();
      LogoutService.timer.cancel();
  }
  </code></pre>
  
  <p>But I get a "Unable to resume activity java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.CountDownTimer android.os.CountDownTimer.start()' on a null object reference" when I try to start any of my activities that contain this code. My application isn't too complicated, it's completely offline, so no web service calls or anything, I just need to go back to my login activity after x minutes. Anyone have any ideas?</p>
  
  <p>Here's the one I used: <a href="https://stackoverflow.com/questions/5705216/auto-logout-after-x-minutes-android">Auto logout after X minutes, Android</a> and <a href="http://androidjug.blogspot.com/2015/10/auto-logout-after-15-minutes-due-to.html" rel="nofollow noreferrer">http://androidjug.blogspot.com/2015/10/auto-logout-after-15-minutes-due-to.html</a></p>