0

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

Ruan_Lopes
  • 1,381
  • 13
  • 18
  • In the first activity, you need to assign the editor reference: `editor = sharedPreferences.edit();` As you have it now, you're calling `sharedPreferences.edit()`, but you're not assigning the return value to your editor reference. – Daniel Nugent Jun 13 '18 at 21:38
  • I fixed that but it still doesnt recognize the String – Saisriram Jun 13 '18 at 21:57

0 Answers0