title: Why does getActivity returns null in Fragment in ViewPager?
body: <p>I have a MainActivity and from there I have ViewPager with two Fragments. I want to change properties of Views of FragmentOne from FragmentTwo. So what I have is a callback method from FragmentTwo to the MainActivity. The callback method is shown here: </p>
  
  <pre><code>@Override
  public void newMessage(String message) {
  
      CalculatorFragment calculatorFragment = (CalculatorFragment) fragmentPagerAdapter.getItem(CALCULATOR_FRAGMENT);
  
      switch (message){
          case SettingsFragment.CHANGE_COLOR:
             //((TextView)view.findViewById(R.id.textViewSum)).setTextColor(Color.RED);
              break;
          case SettingsFragment.NEW_PICTURE:
              calculatorFragment.setBackground();
              break;
          default:
              break;
      }
  }
  </code></pre>
  
  <p>This works all fine. When the user picks a new Picture in FragmentTwo I call the FragmentOne's (CalculatorFragment) method setBackground to set the new picture. Here is how the setBackground method looks like:</p>
  
  <pre><code>public void setBackground(){
      final Bitmap bitmap = new ImageSaver(getActivity()).load();
  
      if(bitmap != null){
          imageViewBackground.setImageBitmap(bitmap);
  
      }
  }
  </code></pre>
  
  <p>Here do I get a nullpointer exception by loading the picture because the getActivity() returns null. </p>
  
  <p><strong><em>EDIT</em></strong>
  Here is the code where I create the ViewPager:</p>
  
  <pre><code>public static final int CALCULATOR_FRAGMENT = 0;
  public static final int SETTINGS_FRAGMENT = 1;
  public static final int FRAGMENTS = 2;
  
  private FragmentPagerAdapter fragmentPagerAdapter;
  
  private ViewPager viewPager;
  
  private List&lt;Fragment&gt; fragments = new ArrayList&lt;Fragment&gt;();
  
  private void initializeFragments(){
      fragments.add(CALCULATOR_FRAGMENT, new CalculatorFragment());
      fragments.add(SETTINGS_FRAGMENT, new SettingsFragment());
  
      fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){
          @Override
          public int getCount() {
              return FRAGMENTS;
          }
          @Override
          public Fragment getItem(final int position) {
              return fragments.get(position);
          }
      };
  
      viewPager = (ViewPager) findViewById(R.id.pager);
  
      viewPager.setAdapter(fragmentPagerAdapter);
  }
  </code></pre>
  
  <p><strong><em>EDIT</em></strong> The Logcat:</p>
  
  <pre><code>08-11 11:38:38.189 21996-21996/com.tomhogenkamp.personalcalc E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.tomhogenkamp.personalcalc, PID: 21996
                                                                             java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content: flg=0x1 }} to activity {com.tomhogenkamp.personalcalc/com.tomhogenkamp.personalcalc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference
                                                                                 at android.app.ActivityThread.deliverResults(ActivityThread.java:4013)
                                                                                 at android.app.ActivityThread.handleSendResult(ActivityThread.java:4063)
                                                                                 at android.app.ActivityThread.access$1400(ActivityThread.java:150)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:168)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5885)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference
                                                                                 at com.tomhogenkamp.personalcalc.CalculatorFragment.setBackground(CalculatorFragment.java:558)
                                                                                 at com.tomhogenkamp.personalcalc.MainActivity.newMessage(MainActivity.java:69)
                                                                                 at com.tomhogenkamp.personalcalc.SettingsFragment.onActivityResult(SettingsFragment.java:118)
                                                                                 at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
                                                                                 at android.app.Activity.dispatchActivityResult(Activity.java:6467)
                                                                                 at android.app.ActivityThread.deliverResults(ActivityThread.java:4009)
                                                                                 at android.app.ActivityThread.handleSendResult(ActivityThread.java:4063) 
                                                                                 at android.app.ActivityThread.access$1400(ActivityThread.java:150) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:168) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5885) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
  </code></pre>
  
  <p>The nullpointer exception only occurs when the app is on background sometimes and then reopening it. So it seems like it has something to do with the Android system that kills processes?</p>
  
  <p>How can I solve this?</p>