Java 首选项管理器和共享首选项
我不确定我在这里做错了什么,希望能得到一些帮助 在我的主要活动的onCreate方法中,我有:Java 首选项管理器和共享首选项,java,android,Java,Android,我不确定我在这里做错了什么,希望能得到一些帮助 在我的主要活动的onCreate方法中,我有: // set the default preferences PreferenceManager.setDefaultValues(context, R.xml.preferences, false); // get the preferences prefs = getPreferences(MODE_PRIVATE); // Load the value
// set the default preferences
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
// get the preferences
prefs = getPreferences(MODE_PRIVATE);
// Load the values or defaults from the SharedPreferences
msMainClockStart = prefs.getLong( "Main_Clock_Minutes", 0 );
useShotClock = prefs.getBoolean( "Use_ShotClock", false );
msShotClockStart = prefs.getLong( "Shot_Clock_Seconds", 20000 );
tvPeriodPrefix = prefs.getString( "Period_Prefix", "P" );
valMaxPeriods = prefs.getInt( "Max_Periods", 4 );
在我的res/xml/preferences.xml文件中,我有以下内容
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="Main_Clock_Minutes"
android:positiveButtonText="SAVE"
android:negativeButtonText="CANCEL"
android:title="Main Clock (minutes)"
android:defaultValue="480000"
android:summary="How many minutes for the main clock."/>
<CheckBoxPreference
android:key="Use_ShotClock"
android:title="Enable Shot Clock"
android:defaultValue="true"/>
<EditTextPreference
android:key="Shot_Clock_Seconds"
android:title="Shot Clock (seconds)"
android:summary="How many seconds for the shot clock."
android:defaultValue="30000"/>
<EditTextPreference
android:key="Period_Prefix"
android:title="Period Prefix (e.g. Q, Shift, Period)"
android:defaultValue="Q"/>
<EditTextPreference
android:key="Max_Periods"
android:title="Maximum number of periods"
android:defaultValue="4"/>
错误为“无法从长字符串强制转换为字符串。但是msMainClockStart是一个长字符串,prefs.getLong()返回一个长字符串,因此我不确定它为什么不起作用。因为您正在使用设置首选项
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
使用
为了检索首选项,而不是
getPreferences()
为了使用PreferenceManager,我发现了两个选项。第一个是我可以创建一个首选项子类,另一个是将其保存为字符串,然后将其转换为长字符串
代码如下:
// set the default preferences
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
// get the preferences
prefs = PreferenceManager.getDefaultSharedPreferences( context );
// Load the values or defaults from the SharedPreferences
msMainClockStart = Long.valueOf( prefs.getString( "Main_Clock_Minutes", "0" ) ) * 60000;
这非常有效。谢谢@Gunnar Karlsson。我编辑了我的原始帖子,其中有一条错误消息,我没有收到。如果有不长的首选项,prefs.getLong(…)会抛出ClassCastException。如果使用prefs.getString(“Main_Clock_Minutes”,0),是否尝试打印该值?尝试在何处/如何打印?很抱歉,这不起作用。我必须获取字符串,然后将其转换为长字符串。
prefs = PreferenceManager.getDefaultSharedPreferences(context)
// set the default preferences
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
// get the preferences
prefs = PreferenceManager.getDefaultSharedPreferences( context );
// Load the values or defaults from the SharedPreferences
msMainClockStart = Long.valueOf( prefs.getString( "Main_Clock_Minutes", "0" ) ) * 60000;