1

I'm trying to switch a toggle button in a preference screen to false once another toggle is turned off. So here when time is flipped I want to turn off name. However it blows up. Anyone see what 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")) {
             // do something
            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):    at com.projectcaruso.naturalfamilyplaning.UserSettingActivity.onSharedPreferenceChanged(UserSettingActivity.java:37)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:475)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:385)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.preference.Preference.tryCommit(Preference.java:1349)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.preference.Preference.persistBoolean(Preference.java:1615)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.preference.TwoStatePreference.setChecked(TwoStatePreference.java:83)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.preference.SwitchPreference$Listener.onCheckedChanged(SwitchPreference.java:54)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.widget.Switch.setChecked(Switch.java:666)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.widget.CompoundButton.toggle(CompoundButton.java:87)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.widget.CompoundButton.performClick(CompoundButton.java:99)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.view.View$PerformClick.run(View.java:17355)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.os.Handler.handleCallback(Handler.java:725)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.os.Handler.dispatchMessage(Handler.java:92)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.os.Looper.loop(Looper.java:137)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at android.app.ActivityThread.main(ActivityThread.java:5041)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at java.lang.reflect.Method.invokeNative(Native Method)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at java.lang.reflect.Method.invoke(Method.java:511)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    05-09 14:40:18.914: E/AndroidRuntime(25005):    at 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>
4

1 に答える 1

3

SwitchPreferenceを使用findPreferenceして渡すことへの参照を取得できますandroid:key

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    boolean mBool = false;

    if (key.equals("cervical_mucus")) {
        // do something
        Context context = getApplicationContext();
        Toast.makeText(context, "Hello toast 1!", Toast.LENGTH_LONG).show();

        SwitchPreference switchPreference = (SwitchPreference) findPreference("mucus_stamps");
        switchPreference.setChecked(mBool);
    }
}

編集

また、リスナーの登録を解除していないようですonPause:

protected void onPause()
{
    super.onPause();
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
于 2013-05-09T15:00:43.417 に答える