Android 对象从方法中序列化自身

Android Object Serializes itself from method

我需要一个包含原始类型设置的对象,以便能够通过从对象调用加载或保存方法来加载和保存 Android 共享首选项。

我创建了一个名为 thePreferences.java 的 class,这个 class 有一些原始字段。我在这个 class 中创建了 2x 个方法来从 sharedPreferences 加载和保存这个 class。请参阅下面我的 class:

中使用的代码
import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.JsonSerializer;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.Serializable;

public class thePreferences extends AppCompatActivity implements Serializable {

    private static final String PREFS_NAME = "thePreferences";
    private static final String PREF_TEXT = "pref_text";
    public static final String TAG = "WPC";

    public boolean loggedIn = false;
    public int deviceId = 0;
    public int userId = 0;
    public String email = "";
    public String deviceAlias = "";

    public thePreferences() {
    }

    public thePreferences(boolean loggedIn, int deviceId, int userId, String email, String deviceAlias) {
        this.loggedIn = loggedIn;
        this.deviceId = deviceId;
        this.userId = userId;
        this.email = email;
        this.deviceAlias = deviceAlias;
    }


    public boolean loadThePreferences(Context c) {

        SharedPreferences pref = c.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        if (pref.contains(PREF_TEXT)) {
            String theJson = pref.getString(PREF_TEXT, null);
            Log.i(TAG, "The JSON string loaded from Shared Preferences : " + theJson);
            thePreferences p = new Gson().fromJson(theJson, thePreferences.class);
            this.deviceAlias = (p.deviceAlias);
            this.deviceId = (p.deviceId);
            this.email = (p.email);
            this.loggedIn = (p.loggedIn);
            return true;

        } else {
            Log.i(TAG, "Could not load SharedPreferences, value does not exist.");
            return false;
        }

    }

    public static boolean saveThePreferences(Context c,thePreferences p){

        Gson gson = new Gson();

        String theJson = gson.toJson(p);
        c.getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
                .edit()
                .putString(PREF_TEXT,theJson)
                .commit();

        return true;
    }




    @Override
    public String toString() {
        return "thePreferences{" +
                "loggedIn=" + this.loggedIn +
                ", deviceId=" + this.deviceId +
                ", userId=" + this.userId +
                ", email='" + this.email + '\'' +
                ", deviceAlias='" + this.deviceAlias + '\'' +
                '}';
    }


}

然后我尝试从另一个 activity 调用它来加载和保存首选项,如下所示:

contextOfApplication = getApplicationContext();
thePreferences preferencesObj = new thePreferences(true,12345,552441,"abc@123.com","theDeivceAlias");
preferencesObj.saveThePreferences(contextOfApplication,preferencesObj);

preferencesObj.loadThePreferences(contextOfApplication);

我无法让它通过 saveThePreferences() 方法执行。它不断崩溃并出现 Whosebug 异常,我不知道为什么。'

有人可以告诉我是否有另一种方法可以做到这一点,或者我哪里做错了吗?

提前致谢

首先,thePreferences 是 class 因此按照惯例应该以大写字母开头。

接下来,您不应该尝试序列化整个 activity。相反,尝试将您需要的数据存储为原始类型,并将它们作为原始类型获取。将整个 class 保存为 json 字符串在这里没有意义。

一种方法:

  1. 创建一个新的 class,其唯一目的是加载和保存您需要的数据。 (就像您的 thePreferences,但将其设为简单的 class,不继承 activity)。

  2. 新的 class 将首选项名称和键保存为静态字符串。

  3. 使用上下文实例化新 class 并将共享首选项预加载为最终成员

  4. 为您需要的单个值提供加载和设置方法

  5. 在您的活动中使用此 class 作为成员或方法区域设置变量

Ps.: 删除 gson 和可序列化的东西

编辑:如果您真的想将数据存储为 json 字符串,则 sharedPreferences 不是要使用的工具。我不建议这样做,但你应该将数据发送到服务器或将其存储在外部数据存储(SD 卡等)上。

将 prefernecesObj 传递给自身

preferencesObj.saveThePreferences(contextOfApplication,preferencesObj);

将函数更改为:

public boolean saveThePreferences(Context c){

    Gson gson = new Gson();

    String theJson = gson.toJson(this);
    c.getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
            .edit()
            .putString(PREF_TEXT,theJson)
            .commit();

    return true;
}

然后这样调用:

preferencesObj.saveThePreferences(contextOfApplication);