Ich versuche, GSON von Google zum ersten Mal in meiner Android App zu verwenden. Ich möchte GSON mit einer Klasse namens UserValues
verwenden, die die meisten meiner ArrayList, booleans, Strings und andere grundlegende Objekte an einem Ort hält. Mein Ziel ist es, eine Instanz von UserValues
in SharedPreferences
zu speichern. Ich schrieb:Fehler bei der Verwendung von GSON mit der eigenen Klasse
Gson gson = new Gson();
String userValuesJSON=gson.toJson(userValues);
getSharedPreferences(myAppKey, MODE_PRIVATE).edit().putString("JSON", userValuesJSON).commit();
Der Fehler erhalte ich:
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)
wenn ich Gson hinzugefügt habe ich folgendes build.gradle
:
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
Alle Brainstorming sehr zu schätzen!
EDIT: Dies ist, wie der Konstruktor wie folgt aussieht:
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_;
...
}
Der Fehler, dass der Standardkonstruktor von UserValues vorschlagen würde ('public UserValues () {}') ist nicht zugänglich. Dies liegt entweder daran, dass eine Person nicht existiert oder sie als privat markiert ist. – rmlan