0

i have a problem with my toggle button and i get always a force close. The code of button:

public void getToggle(View view) {
        boolean on = ((ToggleButton) view).isChecked();
        if (on) {
            PackageManager pm = getPackageManager();
            ComponentName compName = new ComponentName(getApplicationContext(),
            LowBatteryReceiver.class);
            pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
            Toast.makeText(MainActivity.this, "Modalità risparmio attivata", Toast.LENGTH_LONG).show();
            TextView textView = (TextView)findViewById(R.id.risparmiostate);
            textView.setText("Modalità risparmio on");

            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("tgpref", true); // value to store
            editor.commit();

        }else{
            PackageManager pm = getPackageManager();
            ComponentName compName = new ComponentName(getApplicationContext(),
            LowBatteryReceiver.class);
            pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
            Toast.makeText(MainActivity.this, "Modalità risparmio disattivata", Toast.LENGTH_LONG).show();
            TextView textView = (TextView)findViewById(R.id.risparmiostate);
            textView.setText("Modalità risparmio off");

            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("tgpref", false); // value to store
            editor.commit();
        }
    }

and in the onCrerate

public SharedPreferences preferences;
    /** Called when the activity is first created. */
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
        boolean tgpref = preferences.getBoolean("tgpref", true);  //default is true
        if (tgpref = true) //if (tgpref) may be enough, not sure
        {
          togglebutton.setChecked(true);
        }
        else
        {
            togglebutton.setChecked(false);
        }

Any ideas? I tryied everything. Thanks. My logcat doesn't work (i don't know why). Doesn't show me anything.

David_D
  • 1,404
  • 4
  • 31
  • 65

2 Answers2

1

Change

SharedPreferences preferences = getPreferences(MODE_PRIVATE); 

to

 preferences = getPreferences(MODE_PRIVATE);

in onCreate of Activity because you are not initializing preferences instance which u have declared as class level field before using it in getToggle method

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Doesn't work..Ok i have the logcat..I have a fatal error "Unable to start activity" and then "Caused by NullPointerException....MainActivity onCreate(57)" – David_D Jul 03 '13 at 06:02
  • Ok now it works, the problem was that i declared the togglebutton after the check condition. The only one problem now is that the toggle start in "on" state instead "off". Why? – David_D Jul 03 '13 at 06:07
  • man please HELP !! http://stackoverflow.com/questions/17421506/how-to-parse-same-name-tag-in-android-xml-dom-parsing – Altair Jul 03 '13 at 14:22
  • @ρяσѕρєяK : hmm...did you find any solution to this : http://stackoverflow.com/questions/17421506/how-to-parse-same-name-tag-in-android-xml-dom-parsing – Altair Jul 03 '13 at 14:42
1

Try to use

if (tgpref == true)  

Or

if(tgpref)

instead Of

if (tgpref = true).

Try to initialize your TextView textView = (TextView)findViewById(R.id.risparmiostate); in Oncreate().

Also remove SharedPreferences from SharedPreferences preferences = getPreferences(MODE_PRIVATE);As u already declared globally.

Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
  • None of this solutions works :(. Is that possible that the problem is the way i set the toggle? – David_D Jul 03 '13 at 05:58
  • Put your updated code with xml.If u get logact error then also post it. – Bhoomika Brahmbhatt Jul 03 '13 at 08:09
  • I resolved. the problem was that i declared the togglebutton after the check condition.But now the toggle when i start for the first time the application, is set in "on" state and not "off". How can i set as default state? – David_D Jul 03 '13 at 08:37
  • 1
    change boolean tgpref = preferences.getBoolean("tgpref", true); to boolean tgpref = preferences.getBoolean("tgpref", false);. – Bhoomika Brahmbhatt Jul 03 '13 at 11:33