0

I've got a problem using sharedpreferences in my toggle button. First, i declared what i need:

public SharedPreferences preferences;
public WifiManager wifiManager;
ToggleButton wifitoggle;

Then the "action" onClikc

// toggle wifi
    public void wifi (View view) {
        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        boolean wifion = ((ToggleButton) view).isChecked();
        if (wifion) {
            wifiManager.setWifiEnabled(true);
            // Shared preferences
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("togglewifi", true); // value to store
            editor.commit();

        }else{
            wifiManager.setWifiEnabled(false);
            // Shared preferences
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("togglewifi", false); // value to store
            editor.commit();

        }
    }

and in my onCreate i reported the value:

boolean togglewifi = preferences.getBoolean("togglewifi", false);  //default è false
        if (togglewifi) 
        {

            wifitoggle.setChecked(true);
        }
            else
        {
            wifitoggle.setChecked(false);
        }

But when i run the application it crashes. The strange thing is that the logCat say nothing. No errors.. Have i missed something? Thanks for help

David_D
  • 1,404
  • 4
  • 31
  • 65
  • Are you absolutely sure there is nothing in logcat? Try filtering for errors, it is likely you're missing a permission to interact with the WifiManager. – JoxTraex Nov 11 '13 at 11:39
  • http://stackoverflow.com/questions/12639899/shared-preferences-in-android/12640072#12640072 you can refer this. – Akhilesh Mani Nov 11 '13 at 12:00

2 Answers2

1

try this:

SharedPreferences sPref = getSharedPreferences(PREFS_NAME, 0);
Editor editor = sPref.edit();
editor.putBoolean("KEY", true/false);
editor.apply();
WOLVERINE
  • 769
  • 3
  • 12
  • 28
  • where `PREFS_NAME` is like `static final String PREFS_NAME = "MyPreferences";`? – David_D Nov 11 '13 at 11:49
  • anyway: `The method putBoolean(String, boolean) in the type SharedPreferences.Editor is not applicable for the arguments (String, String)` – David_D Nov 11 '13 at 11:50
  • yes, PREFS_NAME is like static final String PREFS_NAME = "MyPreferences"; – WOLVERINE Nov 11 '13 at 12:13
  • only one thing, this your code have i to put over `SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("togglewifi", true); // value to store editor.commit();`? or in onCreate? – David_D Nov 11 '13 at 12:39
0

You need to initialize your sharedprefereneces.

SharedPreferences sharedPreferences=context.getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();

Here, sharedPreferenceName should be a name through which you are going to access the value stored. If it does not match, you won't be able to access the value in sharedpreference.

Bette Devine
  • 1,196
  • 1
  • 9
  • 23