在另一项活动/课程中共享偏好

发布于 2024-12-29 10:46:06 字数 1600 浏览 0 评论 0原文

我编写了一个类来处理共享首选项:

import android.preference.PreferenceManager;
import android.util.Log;
import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceHandler {
    
     Context mContext = null;

        public PreferenceHandler (Context context) {
            mContext = context;
        }
       
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
         
        public void NewGamePrefs(){
             int GameTime=(Integer.parseInt(mContext.getString(R.string.gametime)));
             int Trivianten=(Integer.parseInt(mContext.getString(R.string.trivialocations)));
             SharedPreferences.Editor memory = preferences.edit();
             memory.putInt("GameTime" , GameTime);
             memory.putInt("Score", 0);
             GPSHandler MyGPS=new GPSHandler(mContext);
             memory.putString("StartLocation", MyGPS.getStartLocationID());
             memory.putString("NextLocation", MyGPS.getStartLocationID());
             memory.putString("EndLocation", MyGPS.getEndLocationID());
        commit();
            }
        
        public String getPreferenceString(String KEY){
            return preferences.getString(KEY, "nodata");
        }
        
        public int getPreferenceValue(String KEY){
            return preferences.getInt(KEY, -1);
        }

如果我从活动文件中调用此类:

PreferenceHandler GameMemory=new PreferenceHandler(this);

GameMemory.NewGamePrefs();

我遇到空指针异常,有人知道为什么以及如何解决这个问题吗?

多谢!

I wrote a class to handle the shared preferences:

import android.preference.PreferenceManager;
import android.util.Log;
import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceHandler {
    
     Context mContext = null;

        public PreferenceHandler (Context context) {
            mContext = context;
        }
       
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
         
        public void NewGamePrefs(){
             int GameTime=(Integer.parseInt(mContext.getString(R.string.gametime)));
             int Trivianten=(Integer.parseInt(mContext.getString(R.string.trivialocations)));
             SharedPreferences.Editor memory = preferences.edit();
             memory.putInt("GameTime" , GameTime);
             memory.putInt("Score", 0);
             GPSHandler MyGPS=new GPSHandler(mContext);
             memory.putString("StartLocation", MyGPS.getStartLocationID());
             memory.putString("NextLocation", MyGPS.getStartLocationID());
             memory.putString("EndLocation", MyGPS.getEndLocationID());
        commit();
            }
        
        public String getPreferenceString(String KEY){
            return preferences.getString(KEY, "nodata");
        }
        
        public int getPreferenceValue(String KEY){
            return preferences.getInt(KEY, -1);
        }

If I call this class from the activity file:

PreferenceHandler GameMemory=new PreferenceHandler(this);

GameMemory.NewGamePrefs();

I get a nullpointer exception, anybody any idea why and how to solve this?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2025-01-05 10:46:06

您永远不会初始化首选项。将 preferences 和构造函数更改为:

SharedPreferences preferences;

public PreferenceHandler (Context context) {
    mContext = context;
    preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
}

You never initialize preferences. Change preferences and your constructor to this:

SharedPreferences preferences;

public PreferenceHandler (Context context) {
    mContext = context;
    preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文