gpt4 book ai didi

android - 如何访问 SherlockFragment 中的共享首选项

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:02 25 4
gpt4 key购买 nike

我在选项卡中使用 sherlock fragment ,但我无法访问 sharedpreference 或保存它,我该怎么办?这是我的代码:

 public class SettingsActivity extends SherlockFragment  {

private CheckBox cb1;
private Context context = getActivity();
private SharedPreferences sp;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.settings_layout, container, false);
cb1 = (CheckBox)rootView.findViewById(R.id.cb1);
sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged (CompoundButton cb, boolean isChecked) {
if (isChecked) {
context.getSharedPreferences("some_pref", Context.MODE_PRIVATE)
.edit()
.putBoolean("some_bool_name", true)
.commit();
}
}
});
return rootView;
}
}

如果上述方法不正确,请告诉我如何保存首选项。

LogCat

11-13 18:13:36.907: I/ActivityManager(10626): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp.test/.MainActivity} from pid 10836 11-13 18:13:36.907: D/ActivityManager(10626): TopActivityInfo, pkgName: com.myapp.test activityName: com.myapp.test.MainActivity bstSpecialAppKeyboardHandlingEnabled = false 11-13 18:13:36.907: D/GuidanceScreen(10824): appName: My App, currentPkg: com.myapp.test, event: app_launch, controller: Keyboard 11-13 18:13:36.907: D/ActivityManager(10626): Showing guidance for pkgName: com.myapp.test 11-13 18:13:36.917: D/GuidanceScreen(10824): appName: My App 11-13 18:13:36.987: D/GuidanceScreen(10824): no guidance for com.myapp.test. 11-13 18:13:37.037: D/WindowManager(10626): Sending appClickStats request with data: [package=com.myapp.test, appname=My App, appver=1.0, clickloc=system_server] 11-13 18:13:37.237: I/ActivityManager(10626): Displayed com.myapp.test/.MainActivity: +260ms 11-13 18:13:46.227: D/AndroidRuntime(15464): procName from cmdline: com.myapp.test 11-13 18:13:46.227: E/AndroidRuntime(15464): in writeCrashedAppName, pkgName :com.myapp.test 11-13 18:13:46.227: D/AndroidRuntime(15464): file written successfully with content: com.myapp.test StringBuffer : ;com.myapp.test 11-13 18:13:46.237: E/AndroidRuntime(15464): Process: com.myapp.test, PID: 15464 11-13 18:13:46.237: E/AndroidRuntime(15464): at com.myapp.test.SettingsActivity.onCreateView(SettingsActivity.java:40) 11-13 18:13:46.267: I/ActivityManager(10626): Process com.myapp.test (pid 15464) has died. 11-13 18:13:46.267: W/ActivityManager(10626): Force removing ActivityRecord{4b12da0c u0 com.myapp.test/.MainActivity t17}: app died, no saved state 11-13 18:13:46.287: I/WindowState(10626): WIN DEATH: Window{4b1ac890 u0 com.myapp.test/com.myapp.test.MainActivity}

最佳答案

初始化共享首选项:

private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;

在 onCreateView() 内部:

loginPreferences = getActivity().getSharedPreferences("loginPrefs", getActivity().MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();

保存:

loginPrefsEditor.putString("start_time", yourText);
loginPrefsEditor.commit();

检索:

String dateString = loginPreferences.getString("start_time", "");

关于android - 如何访问 SherlockFragment 中的共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33690317/

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