gpt4 book ai didi

android - SharedPreferences setChecked 崩溃

转载 作者:行者123 更新时间:2023-11-29 18:00:28 26 4
gpt4 key购买 nike

我正在尝试在关闭另一个切换按钮后将首选项屏幕中的切换按钮切换为 false。所以在这里,当时间翻转时,我想关闭名称。然而它爆炸了。有人看到我做错了什么吗?

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);
}

}

错误:

    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)

编辑:

<?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>

最佳答案

您可以使用 findPreference 并传递其 android:key 获取对您的 SwitchPreference 的引用:

@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);
}

关于android - SharedPreferences setChecked 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16464587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com