Android的共享preferences不在主访问,其他活动都很好?都很好、Android、preferences

2023-09-04 02:53:47 作者:无人及我

我保存到我的共享preferences一些数据,但是当我尝试访问它在我的应用程序的主共享preferences文件是空的。如果我叫完全相同的code,从我的任何其他活动,我可以访问这些数据就好了。什么这个问题可能是???任何想法

I have some data saved to my SharedPreferences but when i try to access it in the Main of my app the SharedPreferences file is empty. If i call the same exact code from any of my other Activities i can access the data just fine. any ideas on what the issue could be???

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
    int test = prefs.getInt("PREF_DIFFICULTY", 0);
}

使用Eclipse调试我可以看到,地图支持的preF对象是空的,但是当我查看地图其他活动也充满数据......很怪异。这里是我的清单,以防万一它很重要。

using the eclipse debugger i can see that the map backing the pref object is empty, but when i view that map from other activities it is full of data...very weird. Here is my manifest just in case it matters.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.dawghouse.dd"
android:versionCode="1" android:versionName="1.0">

<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name=".activity.Main" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".activity.BlockOutgoing" android:label="@string/outgoingTitle" />
    <activity android:name=".activity.BlockIncoming" android:label="@string/incomingTitle" />
    <activity android:name=".activity.TimeManager" android:label="@string/timeManagerTitle" />
    <activity android:name=".activity.Preferences" android:label="@string/preferences" />
</application>

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
</manifest>

在此先感谢!

thanks in advance!!

推荐答案

尝试:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

,看看是否能更好地工作。

and see if that works better.