title: new Intent(Context, Class) throws NullPointerException
body: <p>I have the following function:</p>
  
  <pre><code>@Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
      if (s.equals("service_enabled")) {
          boolean shouldStart = sharedPreferences.getBoolean(s, false);
          Intent intent = new Intent(getActivity(), HUD.class);
          if (shouldStart) {  // Turn on
              getActivity().startService(intent);
          } else {            // Turn off
              getActivity().stopService(intent);
          }
  
      }
  }
  </code></pre>
  
  <p>The function is called when a change is done to my <code>PreferenceFragment</code>. The service <code>Hud</code> is (probably) running if <code>shouldStart == false</code>, and then I want to stop it. This works as expected if you just click the option twice; the service starts, and stops. However, if you start it, go back to the previous <code>activity</code>, and enter this <code>fragment</code> again, and try to disable it, the following happends: </p>
  
  <pre><code>01-25 22:22:44.848  32548-32548/net.hath.drawcut E/AndroidRuntime﹕ FATAL EXCEPTION: main
  java.lang.NullPointerException
          at android.content.ComponentName.&lt;init&gt;(ComponentName.java:75)
          at android.content.Intent.&lt;init&gt;(Intent.java:3558)
          at net.hath.drawcut.ui.fragment.PrefsFragment.onSharedPreferenceChanged(PrefsFragment.java:35)
          at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:475)
          at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:385)
          at android.preference.Preference.tryCommit(Preference.java:1349)
          at android.preference.Preference.persistBoolean(Preference.java:1615)
          at android.preference.TwoStatePreference.setChecked(TwoStatePreference.java:83)
          at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:69)
          at android.preference.Preference.performClick(Preference.java:949)
          at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
          at android.widget.AdapterView.performItemClick(AdapterView.java:298)
          at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
          at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
          at android.widget.AbsListView$1.run(AbsListView.java:3423)
          at android.os.Handler.handleCallback(Handler.java:725)
          at android.os.Handler.dispatchMessage(Handler.java:92)
          at android.os.Looper.loop(Looper.java:137)
          at android.app.ActivityThread.main(ActivityThread.java:5227)
          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:795)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Line 35 is <code>Intent intent = new Intent(getActivity(), HUD.class);</code>.</p>
  
  <p>What is happening here? </p>