I'm trying to store a user's submitted name with a button click to be retrieved and used in another activity. Currently I'm getting an error that I haven't been able to resolve and any help would be appreciated.
The error is Cannot resolve method 'getSharedPreferences(java.lang.String, int)'
Here's my code
package net.androidbootcamp.gamecompanion;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.DecimalFormat;
import static android.app.PendingIntent.getActivity;
import static android.content.Context.MODE_PRIVATE;
public class SettingsActivity extends AppCompatActivity {
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
final EditText enterName1 = (EditText) findViewById(R.id.enterName1);
final EditText enterName2 = (EditText) findViewById(R.id.enterName2);
Button btnOK = (Button) findViewById(R.id.btnOK);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
btnOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String n = enterName1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.commit();
// Toast.makeText(SettingsActivity.this, "Player name set!", Toast.LENGTH_LONG).show();
}
});
}
}