0

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();


        }
        });


}

}

John
  • 141
  • 2
  • 10
  • Refer to this answer on how to set and retrieve SP: https://stackoverflow.com/a/23024962/5065318 – vivek verma Apr 09 '18 at 02:30
  • what error is showing? – iamkdblue Apr 09 '18 at 02:32
  • Cannot resolve method 'getSharedPreferences(java.lang.String, int)' – John Apr 09 '18 at 02:34
  • Is that the only error you're getting? Or are there others, as well? For example, something wrong with the `AppCompatActivity` class, or an "Unexpected something-or-other" error. – Mike M. Apr 09 '18 at 02:37
  • This is the only error I'm getting, with getSharedPreferences outlined in red as the root of the error – John Apr 09 '18 at 02:38
  • Hmm, well, that method call is correct. Maybe try cleaning/rebuilding the project. If that doesn't work, please post the entire class, imports and everything. – Mike M. Apr 09 '18 at 02:41
  • It didn't work, I updated with the full class – John Apr 09 '18 at 03:02
  • I don't see anything wrong, but you've got some unnecessary imports. Maybe they're confusing your IDE. You might try removing the unused ones, particularly `android.app.Activity`. FWIW, the whole class, as is, works just fine in my IDE. – Mike M. Apr 09 '18 at 04:27

2 Answers2

0
SharedPreferences shredpreferences=getApplicationContext().getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
Rohit Suthar
  • 586
  • 3
  • 19
0

You need to call getSharedPreferences() on a Context.

So, change

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

To

sharedpreferences = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Cvarier
  • 161
  • 13