Why isnt my SharedPreferences working?

27 Views Asked by At

Here is my code:

public class HeadBoy extends AppCompatActivity {
public static final String SHARED_PREFS = "shareactivity";
public  static final String HEAD_BOY_ONE = "count";
public static final String HEAD_BOY_TWO = "counttwo";
public SharedPreferences sharedPreferences ;
public SharedPreferences.Editor editor;
int hb1;
int hb2;
@Override
protected void onResume() {
    hideNavigationBar();
    super.onResume();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_head_boy);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);


    final Button hbone = (Button) findViewById(R.id.hbOne);
    final Button hbtwo = (Button) findViewById(R.id.hbTwo);
    hbone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hb1 = hb1 + 1;
            Intent intent = new Intent(HeadBoy.this,HeadGirl.class);
            startActivity(intent);
        }
    });
    hbtwo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hb2 = hb2 + 1;
            Intent intent = new Intent(HeadBoy.this,HeadGirl.class);
            startActivity(intent);
        }
    });
    hideNavigationBar();
    sharedPreferences.edit();
    editor.putInt(HEAD_BOY_ONE,hb1);
    editor.putInt(HEAD_BOY_TWO,hb2);
    editor.commit();
}


  public class Results extends AppCompatActivity {
@Override
protected void onResume() {
    super.onResume();
    hideNavigationBar();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    sharedPreferences.getInt(HEAD_BOY_ONE,0);

These are the two activities Im trying to pass data between.I need to use the integer in the first activity in the Results one,I believe I did everything needed for a SharedPreference,but when I use the value in the other activity its red and tells me it doesnt recognize it,in this case HEAD_BOY_ONE. Please help

0

There are 0 best solutions below