Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Activity Lifecycle Introducing SharedPreferences Saving and Retrieving Data with SharedPreferences

./MainActivity.java:25: error: method putBoolean in class Editor cannot be applied to given types;

In case you hadn't guess I'm on task 3 or 4 for the "Saving and Retrieving Data with SharedPreferences" challenge:

Challenge Task 3 of 4

Override the onPause method. Then save the value of the CheckBox. Hint: Use isChecked() to get the value of the CheckBox, and use 'putBoolean' to store the value.

Was there anything about using putBoolean or isChecked() in the videos?

.

Specifically I'm looking at time index 02:54 of this video

(without the ampersand pound sign thirteen at the end):

https://teamtreehouse.com/library/android-activity-lifecycle/introducing-sharedpreferences/saving-and-retrieving-data-with-sharedpreferences

.

Note: I also looked at the code in the zip download and didn't see anything about isChecked() or putBoolean in that code either...?


Here's what I tried (which got the error noted in the title at the top of this forum post),

MainActivity.java
import android.view.View;
import android.os.Bundle;

public class MainActivity extends Activity {

  public CheckBox mCheckBox;
  private static final String PREFS_FILE = "com.teamtreehouse.sharedpreferencesapp.preferences";
  private static final String KEY_CHECKBOX = "key_checkbox";
  private SharedPreferences mSharedPreferences;
  private SharedPreferences.Editor mEditor;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
    mEditor = mSharedPreferences.edit();
    mCheckBox = (CheckBox) findViewById(R.id.checkBox);
  }

  @Override
  protected void onPause() {
    super.onPause();
    mEditor.putBoolean(mCheckBox.isChecked());
    mEditor.apply();
  }

}

Note: If you delete the onPause code section from the code above,

you get the code below, which at least gets you through tasks 1 and 2 of 4:

MainActivity.java
import android.view.View;
import android.os.Bundle;

public class MainActivity extends Activity {

  public CheckBox mCheckBox;
  private static final String PREFS_FILE = "com.teamtreehouse.sharedpreferencesapp.preferences";
  private static final String KEY_CHECKBOX = "key_checkbox";
  private SharedPreferences mSharedPreferences;
  private SharedPreferences.Editor mEditor;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
    mEditor = mSharedPreferences.edit();
    mCheckBox = (CheckBox) findViewById(R.id.checkBox);
  }
}

Some related links I looked at

(without the ampersand pound sign thirteen at the end --why does it do that???!!!):

http://stackoverflow.com/questions/31633851/how-to-save-status-of-checkbox-in-android

http://stackoverflow.com/questions/7941770/save-checkbox-value

http://www.dahuatu.com/G9wRroZ8wM.html

3 Answers

Brad Mullett
Brad Mullett
76,804 Points

You're missing the key in you're key value air. It should look like this:

  @Override
  protected void onPause() {
    super.onPause();
    mEditor.putBoolean(KEY_CHECKBOX, mCheckBox.isChecked());
    mEditor.apply();
  }
Brad Mullett
Brad Mullett
76,804 Points

You'll need to retrieve the checkbox value from shared preferences using getBoolean and apply it to the checkbox using setChecked. Be sure to set a default value of false to the boolean in case nothing is saved.

The result should look like this:

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
    mEditor = mSharedPreferences.edit();
    mCheckBox = (CheckBox) findViewById(R.id.checkBox);
    Boolean mIsChecked = mSharedPreferences.getBoolean(KEY_CHECKBOX, false);
    mCheckBox.setChecked(mIsChecked);
  }

Thanks Brad!

Now I can finally move on to task 4 of 4:

Lastly, retrieve the saved value in onCreate and set the state of the CheckBox. If there isn't a saved value, the CheckBox should NOT be checked. Hint: Use the setChecked method.

Yogesh Kumar
Yogesh Kumar
3,719 Points

Hi I stucked on the task 4 of 4: Can you help please?