title: SharedPreferences NullPointerException in DialogFragment
body: <p>I have some trouble working with SharedPreferences in DialogFragment. And I keep getting NullPointerException in line <code>sharedPreferences = getActivity().getSharedPreferences("pref", 0);</code>.
  Here is may basic code.</p>
  
  <pre><code>public class ADialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
      SharedPreferences sharedPreferences;
      public ADialogFragment(int a) {
          sharedPreferences = getActivity().getSharedPreferences("pref", 0);
          if (a == 0) {
              saveToPref(0);
          } else if (a == 1) {
              saveToPref(1);
          } else saveToPref(2);
      }
      private void saveToPref(int itemInt) {
          SharedPreferences.Editor editor = sharedPreferences.edit();
          editor.putInt(getString(R.string.item), itemInt);
          editor.commit();
      }
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      ...
      }
  }
  </code></pre>
  
  <p>How can i fix it?</p>