android - SharedPreferences setChecked crash -


i'm trying switch toggle button in preference screen false once toggle turned off. here when time flipped want turn off name. blows up. see i'm doing wrong?

import android.content.context; import android.content.sharedpreferences; import android.content.sharedpreferences.onsharedpreferencechangelistener; import android.os.bundle; import android.preference.preferenceactivity; import android.widget.toast; import android.widget.togglebutton;   public class usersettingactivity extends preferenceactivity implements onsharedpreferencechangelistener{     sharedpreferences mpreferences;     boolean frequency;       @suppresswarnings("deprecation")     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         addpreferencesfromresource(r.xml.settings);     }       @override     public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) {         boolean mbool = false;          if (key.equals("cervical_mucus")) {              //             context context = getapplicationcontext();             toast.maketext(context, "hello toast 1!", toast.length_long).show();              togglebutton mtoggle = (togglebutton)findviewbyid(r.id.mucus_stamps);             mtoggle.setchecked(mbool);          }     }      protected void onpause()     {         super.onpause();         getpreferencescreen().getsharedpreferences().registeronsharedpreferencechangelistener(this);     }      protected void onresume()     {         super.onresume();         getpreferencescreen().getsharedpreferences().registeronsharedpreferencechangelistener(this);     }  } 

error:

    05-09 14:40:18.914: e/androidruntime(25005): fatal exception: main     05-09 14:40:18.914: e/androidruntime(25005): java.lang.nullpointerexception     05-09 14:40:18.914: e/androidruntime(25005):    @ com.projectcaruso.naturalfamilyplaning.usersettingactivity.onsharedpreferencechanged(usersettingactivity.java:37)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.app.sharedpreferencesimpl$editorimpl.notifylisteners(sharedpreferencesimpl.java:475)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.app.sharedpreferencesimpl$editorimpl.apply(sharedpreferencesimpl.java:385)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.preference.preference.trycommit(preference.java:1349)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.preference.preference.persistboolean(preference.java:1615)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.preference.twostatepreference.setchecked(twostatepreference.java:83)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.preference.switchpreference$listener.oncheckedchanged(switchpreference.java:54)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.widget.compoundbutton.setchecked(compoundbutton.java:126)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.widget.switch.setchecked(switch.java:666)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.widget.compoundbutton.toggle(compoundbutton.java:87)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.widget.compoundbutton.performclick(compoundbutton.java:99)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.view.view$performclick.run(view.java:17355)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.os.handler.handlecallback(handler.java:725)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.os.handler.dispatchmessage(handler.java:92)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.os.looper.loop(looper.java:137)     05-09 14:40:18.914: e/androidruntime(25005):    @ android.app.activitythread.main(activitythread.java:5041)     05-09 14:40:18.914: e/androidruntime(25005):    @ java.lang.reflect.method.invokenative(native method)     05-09 14:40:18.914: e/androidruntime(25005):    @ java.lang.reflect.method.invoke(method.java:511)     05-09 14:40:18.914: e/androidruntime(25005):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793)     05-09 14:40:18.914: e/androidruntime(25005):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560)     05-09 14:40:18.914: e/androidruntime(25005):    @ dalvik.system.nativestart.main(native method) 

edit:

<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" >      <preferencecategory          android:title="@string/pref_user_profile"          android:textsize="20px">          <switchpreference                  android:title="@+string/pref_frequency"                 android:summary="@+string/pref_frequency_summary"                 android:key="frequency"                  android:defaultvalue="true"                 android:layout="@layout/pref_layout"/>          <switchpreference                  android:title="@+string/pref_time"                 android:summary="@+string/pref_time_summary"                 android:key="time"                 android:defaultvalue="true"                 android:layout="@layout/pref_layout"                 android:dependency="frequency"/>          <switchpreference                   android:title="@+string/pref_symptothermal"                 android:summary="@+string/pref_symptothermal_summary"                 android:key="symptothermal"                 android:defaultvalue="true"                 android:layout="@layout/pref_layout"/>          <switchpreference                  android:title="@+string/pref_cervical_mucus"                 android:summary="@+string/pref_cervical_mucus_summary"                 android:key="cervical_mucus"                 android:defaultvalue="true"                 android:layout="@layout/pref_layout"                 android:disabledependentsstate="false"/>              <switchpreference                  android:id="@+id/mucus_stamps"                 android:title="@+string/pref_mucus_stamps"                 android:summary="@+string/pref_mucus_stamps_summary"                 android:key="mucus_stamps"                 android:defaultvalue="true"                 android:layout="@layout/pref_layout"                 android:dependency="cervical_mucus"/>          <switchpreference                  android:title="@+string/pref_fertile_infertile"                 android:summary="@+string/pref_fertile_infertile_summary"                 android:key="fertile_infertil"                  android:defaultvalue="true"                 android:layout="@layout/pref_layout"                 android:dependency="cervical_mucus"/>     </preferencecategory>  </preferencescreen> 

you can reference switchpreference using findpreference , passing android:key:

@override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) {     boolean mbool = false;      if (key.equals("cervical_mucus")) {         //         context context = getapplicationcontext();         toast.maketext(context, "hello toast 1!", toast.length_long).show();          switchpreference switchpreference = (switchpreference) findpreference("mucus_stamps");         switchpreference.setchecked(mbool);     } } 

edit

it appears not unregistering listener in onpause:

protected void onpause() {     super.onpause();     getpreferencescreen().getsharedpreferences().unregisteronsharedpreferencechangelistener(this); } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -