尝试将GSON与自己的类一起使用时出错 [英] Error trying to use GSON with own class

查看:414
本文介绍了尝试将GSON与自己的类一起使用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在Android应用中首次使用Google的GSON。我想用一个名为 UserValues 的类来使用GSON,它将我的大部分ArrayList,布尔值,字符串和其他基本对象保存在一个地方。我的目标是将 UserValues 的实例保存到 SharedPreferences 中。我写道:

  Gson gson = new Gson(); 
String userValuesJSON = gson.toJson(userValues);
getSharedPreferences(myAppKey,MODE_PRIVATE).edit()。putString(JSON,userValuesJSON).commit();

我得到的错误:

<$ p $ java.lang.SecurityException:无法在java.lang.reflect.Constructor.setAccessible(Constructor.java:336)
处访问
方法构造函数com.google .gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:97)
,位于com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:79)
,位于com.google.gson.internal .bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:71)
at com.google.gson.Gson.getAdapter(Gson.java:356)

当我添加GSON时,我将以下内容添加到 build.gradle

 存储库{
mavenCentral()
}

依赖关系{
编译'com.google.code.gson :gson:2.2.4'
}

任何头脑风暴都非常感激!



编辑:这是如何构造看起来像:

  public class UserValues implements Serializable {

私人整数周期;
私人浮动费;
private ArrayList<布尔> theBooleans = new ArrayList<>();
private ArrayList< Integer> theIntegers = new ArrayList<>();
private static final ArrayList< String> theIntegerKeys = new ArrayList<>();
private static final ArrayList< String> theBooleanKeys = new ArrayList<>();
public static final String myAppKey =投资者游乐场;
private static final ArrayList< Integer> theIntDefValues = new ArrayList<>();
private ArrayList< Float>数组列表;
private ArrayList< ArrayList< Integer>>日期;
private ArrayList< Float> strategyResult;
private int theCurrentFragment;

private int theCurrentGraph;

上下文环境;


public UserValues(Context context_){
context = context_;
...

}


解决方案 div>

在类
中添加一个空的构造函数,因为这只会用于设置值,如果上下文未初始化,这应该不会引起任何问题。

  public UserValues(){

}


I'm trying to use Google's GSON for the first time in my Android app. I want to use GSON with a class called UserValues, which keeps most of my ArrayList, booleans, Strings and other basic objects in one place. My goal is to save an instance of UserValues into SharedPreferences. I wrote:

Gson gson = new Gson();
        String userValuesJSON=gson.toJson(userValues);
        getSharedPreferences(myAppKey, MODE_PRIVATE).edit().putString("JSON", userValuesJSON).commit();

The error I get:

java.lang.SecurityException: Can't make method constructor accessible
            at java.lang.reflect.Constructor.setAccessible(Constructor.java:336)
            at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:97)
            at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:79)
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:71)
            at com.google.gson.Gson.getAdapter(Gson.java:356)

when I added GSON I added the following to build.gradle:

repositories {
    mavenCentral()
}

dependencies {   
    compile 'com.google.code.gson:gson:2.2.4'
}

Any brainstorming greatly appreciated!

EDIT: This is how the constructor looks like:

public class UserValues implements Serializable {

    private Integer period;
    private Float fee;
    private ArrayList<Boolean> theBooleans = new ArrayList<>();
    private ArrayList<Integer> theIntegers = new ArrayList<>();
    private static final ArrayList<String> theIntegerKeys = new ArrayList<>();
    private static final ArrayList<String> theBooleanKeys = new ArrayList<>();
    public static final String myAppKey = "Investor Playground";
    private static final ArrayList<Integer> theIntDefValues = new ArrayList<>();
    private ArrayList<Float> arrayList;
    private ArrayList<ArrayList<Integer>> theDates;
    private ArrayList<Float> strategyResult;
    private int theCurrentFragment;

    private int theCurrentGraph;

    Context context;


    public UserValues(Context context_) {
        context=context_;
    ...

    }

解决方案

add an empty constructor to the class as this will only be used to set values this should not cause any issues if the context is not initialised

public UserValues() {

}

这篇关于尝试将GSON与自己的类一起使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆