【问题标题】:addPreferencesFromResource makes sharedPreferences not workaddPreferencesFromResource 使 sharedPreferences 不起作用
【发布时间】:2012-01-07 11:01:00
【问题描述】:

所以可能是另一个新问题:)

我有一个Activity、一个AppWidgetProvider 和一个PreferenceActivity。在PreferenceActivity 中,我有一个ListPreference,我用它来定义小部件的文本大小。 sharedPreferences 在主活动开始时加载没有问题,但是一旦PreferenceActivity 运行,我就不能再在主活动中正确检索sharedPreference 值。我尝试直接在PreferenceActivity 中检索sharedPreference 值,同样的事情发生了:由于某种原因,当我尝试从sharedPreferences 获取值之前,我调用addPreferencesFromResource(R.drawable.settings);(在评论'测试1'),它说:

“onStart() 1, size: small”和“Small works!”

即有用。但是当我尝试在资源调用之后(在评论“测试 2”处)检索值时,它说:

“onStart() 2, size: small”和“它不起作用……”

即它不起作用。我不知道为什么。显然它得到了sharedPreference 的值,因为它说当前textWidgetSize 在这两种情况下都等于“小”,但由于某种原因,它不认为在资源调用之后“小”=“小”。各位大佬知道怎么回事吗?

这是 PreferenceActivity 代码:

package dk.mfoller.android.basicnote;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;
import dk.mfoller.android.basicnote.R;

public class BasicNoteSettings extends PreferenceActivity{

    String widgetTextSize = "small";
    boolean widgetLineCounter = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Calls a function to get the preferences
        getPrefs();

        // Test 1
        makeToast("onStart() 1, size: " + widgetTextSize);
        if(widgetTextSize == "small") {
            makeToast("Small works!");
        } else if(widgetTextSize == "medium") {
            makeToast("Medium works!");
        } else if(widgetTextSize == "large") {
            makeToast("Large works!");
        } else {
            makeToast("It doesn't work ...");
        }

        // Gets the preference layout from xml
        addPreferencesFromResource(R.drawable.settings);

        // Calls a function to get the preferences
        getPrefs();

        // Test 2
        makeToast("onStart() 2, size: " + widgetTextSize);
        if(widgetTextSize == "small") {
            makeToast("Small works!");
        } else if(widgetTextSize == "medium") {
            makeToast("Medium works!");
        } else if(widgetTextSize == "large") {
            makeToast("Large works!");
        } else {
            makeToast("It doesn't work ...");
        }

    }

    // A function to get the preferences    
    private void getPrefs() {
        // Gets data from the shared preferences
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        widgetTextSize = prefs.getString("text_size_list", "small");
        widgetLineCounter = prefs.getBoolean("line_counter_cbox", true);
    }

    // A function to display a popup
    private void makeToast(String popup) {
        Toast.makeText(this, popup, Toast.LENGTH_SHORT).show();
    }

}

这是 xml 文档:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="basicnote_settings"
    android:title="@string/settings_label">

    <PreferenceCategory android:title="Text size">
        <ListPreference android:key="text_size_list"
        android:title="Widget text size"
        android:summary="@string/text_size_summary"
        android:entries="@array/text_size_options"
        android:entryValues="@array/text_size_values"
        android:defaultValue="small" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Other settings">
        <CheckBoxPreference android:key="line_counter_cbox"
            android:title="Line counter"
            android:summary="@string/hidden_lines_summary"
            android:defaultValue="true" />
    </PreferenceCategory>

</PreferenceScreen>

这是我的资源字符串:

<?xml version="1.0" encoding="utf-8"?>

<!-- Defines various resources -->
<resources>
    <string name="app_name">basicNote</string>
    <string name="note_hint">Tap to add some notes ...</string>
    <string name="fake_load">Loading notes ...</string>

    <string name="settings_label">basicNote settings</string>

    <string name="text_size_summary">Set the size of the widget text</string>
    <string-array name="text_size_options">
        <item>Small (12sp)</item>
        <item>Medium (13sp)</item>
        <item>Large (15sp)</item>
    </string-array>
    <string-array name="text_size_values">
        <item>small</item>
        <item>medium</item>
        <item>large</item>
    </string-array>

    <string name="hidden_lines_summary">Show/hide the number of lines not shown in the widget</string>
</resources>

【问题讨论】:

    标签: android sharedpreferences preferenceactivity


    【解决方案1】:

    尝试将字符串比较中的所有 == 更改为 equals()。例如:

    if(widgetTextSize.equals("small"))
    {
    }
    

    看看this的文章

    【讨论】:

      【解决方案2】:

      您在 String 对象上使用 ==。请改用 .equals(yourString)。

      【讨论】:

      • 你们太棒了!这解决了它 - 当然。我花了几天的时间试图解决这个问题。非常感谢!
      猜你喜欢
      • 1970-01-01
      • 2018-11-14
      • 2016-12-03
      • 2016-10-11
      • 2020-06-07
      • 2015-02-09
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多