【发布时间】:2018-02-10 17:10:21
【问题描述】:
我正在尝试将 ArrayList 保存到 Java 中的 Shared Preferences,但我收到了 Illegal Argument Exception:
Caused by: java.lang.IllegalArgumentException: class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.Gson.toJson(Gson.java:661)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
at de.onwukwe.chinaedu.remind.Start.start(Start.java:172)
at de.onwukwe.chinaedu.remind.Start.go(Start.java:138)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6261)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
我只是不知道我做错了什么。我有一个 ArrayList 存储点类型的对象:
ArrayList<Dot> generatedSequence=new ArrayList<>();
String json = new Gson().toJson(generatedSequence);
editor.putString("GS", json);
editor.commit();
这是我的自定义类点:
public class Dot {
//unique ID of a dot, cannot be changed
public int ID;
//colour of a dot, can be changed through actions
String colour ;
//the real image on screen
ImageView dot;
//constructor
public Dot(int id, ImageView dot, String colour){
Log.i("KonstruktorID: ", String.valueOf(id));
this.ID=id;
this.dot=dot;
this.colour = colour;
dotAppearance();
}
/**
* This is a gettter method it returns the ID of the Dot
* @return ID
*/
public int getID(){
return ID;
}
/**
* This is a gettter method it returns the colour of the Dot
* @return colour
*/
public String getColour (){
return colour;
}
/**
* This method sets the colour of a dot
* @param colour
*/
public void setColour(String colour){
this.colour=colour;
}
/**
* This method sets the appearance of a dot, so the colour and the size. It depends on the given colour and the current level
*/
public void dotAppearance(){
//TODO add size changing
switch (colour){
case "yellow":
dot.setImageResource(R.drawable.yellow_big_point);
break;
case "blue":
dot.setImageResource(R.drawable.blue_big_point);
break;
case "red":
dot.setImageResource(R.drawable.red_big_point);
break;
case "orange":
dot.setImageResource(R.drawable.orange_big_point);
break;
case "pink":
dot.setImageResource(R.drawable.pink_big_point);
break;
default:
break;
}
}
/**
* this function should compute the color specific action of a dot and return the resultSequence
* @return
*/
public ArrayList action(){
return null;
}
点被添加到列表中,然后列表应保存到共享首选项。但它不会起作用。当我从 ArrayList 中删除通用类型点时,不会发生错误,但这不是解决方案。 如果有人可以帮助我,那就太好了,在此先感谢。
【问题讨论】:
-
也许您可以添加更详细的堆栈跟踪,以便我们更好地帮助您
-
你在哪里使用这个字段 mChangingConfigurations ?
-
@nimi0112 我不知道它在哪里使用。我没有在我的代码中使用这个变量,一定是 GSON 方面的东西
-
这里有什么解决办法吗?仅在发布版本中发生
标签: java android json arraylist gson