title: Singleton Method Gives Error in OnResum (Android)
body: <p>I have a problem that I could not understand why it is happening.</p>
  
  <p>I have a fragment, and in the fragment I have the on resume method as below:</p>
  
  <pre><code>@Override
  public void onResume(){
      super.onResume();
  
      System.out.println("number 1");
  
      int s = ((StarterApplication) this.getActivity().getApplication()).getRefreshNotificationVar();
  
      System.out.println("number 2");
      s = s + 1;
  
      System.out.println("number 3");
      ((StarterApplication) this.getActivity().getApplication()).setRefreshNotificationVar(s);
  
      System.out.println("number 4");
      System.out.println(s);
  
  
  }
  </code></pre>
  
  <p>And my singletons are as below:</p>
  
  <pre><code>private int refreshNotificationVar;
  
  public int getRefreshNotificationVar() {
  
      System.out.println("check out here");
      return refreshNotificationVar;
  }
  
  public void setRefreshNotificationVar(int refreshNotificationVar) {
      this.refreshNotificationVar = refreshNotificationVar;
      System.out.println(refreshNotificationVar+" is my number");
  
  }
  </code></pre>
  
  <p>And I call this fragment once in the oncreate of the activity as below, which works fine. The singleton methods and else.</p>
  
  <pre><code>newsFeedFragment fragment2 = new newsFeedFragment();
  android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
  fragmentTransaction2.add(R.id.fragment_container, fragment2, "news_feed_fragment");
  fragmentTransaction2.addToBackStack(null);
  fragmentTransaction2.commit();
  </code></pre>
  
  <p>And I call this fragment again with a button as below. However here I start to get the error when the singleton method starts.</p>
  
  <pre><code>newsFeedFragment fragment = new newsFeedFragment();
  android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  fragmentTransaction.hide(getSupportFragmentManager().findFragmentById(R.id.fragment_container));
  fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag("notifications_fragment"));
  fragmentTransaction.show(getSupportFragmentManager().findFragmentByTag("news_feed_fragment"));
  fragmentTransaction.addToBackStack(null);
  fragmentTransaction.commit();
  fragment.onResume();
  </code></pre>
  
  <p>When the fragment loads for the first time, it works fine, but when I show the fragment and start the onResume method manually, I start to get the error as below:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
  java.lang.IllegalStateException: Could not execute method for android:onClick
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
      at android.view.View.performClick(View.java:3511)
      at android.view.View$PerformClick.run(View.java:14105)
      at android.os.Handler.handleCallback(Handler.java:605)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:4424)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
      at android.view.View.performClick(View.java:3511) 
      at android.view.View$PerformClick.run(View.java:14105) 
      at android.os.Handler.handleCallback(Handler.java:605) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4424) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
      at dalvik.system.NativeStart.main(Native Method) 
   Caused by: java.lang.NullPointerException
      at com.my_last.mylast.newsFeedFragment.onResume(newsFeedFragment.java:223)
      at com.my_last.mylast.MainActivity.go_to_main_view(MainActivity.java:565)
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
      at android.view.View.performClick(View.java:3511) 
      at android.view.View$PerformClick.run(View.java:14105) 
      at android.os.Handler.handleCallback(Handler.java:605) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4424) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
      at dalvik.system.NativeStart.main(Native Method) 
  </code></pre>
  
  <p>Does anyone has any idea about this?</p>