title: getSharedPreferences error in non-activity class
body: <p>I realize this issue has been touched on numerous times but nothing I try is working for me. I still get errors when trying to access <code>SharedPreferences</code>.</p>
  
  <p>From the main Activity (McsHome) I am launch a variety of Dialogs to help the user add a location. </p>
  
  <p>The first Dialog is below, this simply pops up a message stating a location needs to be added (PopupMessage.java):</p>
  
  <pre><code>public class PopupMessage extends DialogFragment {
  
      String message = "";
      AddLocation addLocation;
  
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
  
          addLocation = new AddLocation();
  
          // Use the Builder class for convenient dialog construction
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          builder.setMessage(message)
                 .setPositiveButton("Add Location", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                         addLocation.show(getFragmentManager(), "PopupMsgFragment");
                     }
                 })
                 .setNegativeButton("Ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                //
              };
          });
          // Create the AlertDialog object and return it
          return builder.create();
      }
  }
  </code></pre>
  
  <p>This gives the user an option to add a location, when that button is clicked another dialog pops up (AddLocation.java):</p>
  
  <pre><code>public class AddLocation extends DialogFragment {
  
      EditText mcsDomain;
      EditText friendlyName;
      EditText password;
      ProcessLocation addLoc;
      String message = "";
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
  
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          LayoutInflater inflater = getActivity().getLayoutInflater();
          View layout = inflater.inflate(R.layout.add_location_dialog, null); // Pass null as the parent view because its going in the dialog layout
          mcsDomain = (EditText) layout.findViewById(R.id.mcsDomain);
          friendlyName = (EditText) layout.findViewById(R.id.friendlyName);
          password = (EditText) layout.findViewById(R.id.password);
  
          builder.setView(layout)
                  .setTitle("Add/Update Location")
          // Add action buttons
                 .setPositiveButton("Add/Update", new DialogInterface.OnClickListener() {
                     @Override
  
                     public void onClick(DialogInterface dialog, int id) {
  
                         // Passes the chosen location parameters to the ProcessLocation class
                         addLoc.processLocation(mcsDomain.getText().toString(),friendlyName.getText().toString(),password.getText().toString());
  
                     }
                 })
                 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
  
                     }
                 });      
          return builder.create();
      }
  </code></pre>
  
  <p>The AddLocation.java uses an XML layout which includes 3 EditText fields. The values of these are passed on to a third Class, ProcessLocation.java which includes the method <code>processLocation()</code>.</p>
  
  <pre><code>public class ProcessLocation {
  
  
      SharedPreferences domainToName;
      SharedPreferences nameToDomain;
  
  
       public void processLocation(String domain, String name, String password) {
  
              domainToName = getSharedPreferences("domainToName",  MODE_PRIVATE);
              nameToDomain = getSharedPreferences("nameToDomain",  MODE_PRIVATE);
          //  final Editor domainEdit = domainToName.edit();
          //  final Editor nameEdit = nameToDomain.edit();
  
              if (nameToDomain.contains(name)) {
                     System.out.println("Name Doesn't Exist");
                  }
  
          }
  
  }
  </code></pre>
  
  <p>I'm getting an error on the <code>MODE_PRIVATE</code>, I believe related to Context. I've been playing around with context for hours with no luck (or understanding). I know I'm popping up a couple of dialogs in a row. If I add "extends Activity" the error goes away but then the app crashes when trying to <code>getSharedPreferences</code>.</p>
  
  <p>From going through the other posts I'm sure it's to do with passing the context from my McsHome.java activity but everything I've tried has failed. </p>