IllegalArgumentException多个json字段名称

时间:2018-02-10 17:10:21

标签: java android json arraylist gson

我正在尝试将ArrayList保存到Java中的共享首选项,但我收到了非法的参数异常:

 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,它存储Dot类型的对象:

ArrayList<Dot> generatedSequence=new ArrayList<>();
String json = new Gson().toJson(generatedSequence);
            editor.putString("GS", json);
            editor.commit();

这是我的自定义类Dot:

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的泛型Type Dot时,不会发生错误,但这不是解决方案。 如果有人能帮助我那会很棒,请提前谢谢。

2 个答案:

答案 0 :(得分:0)

您应该使用Gson的toJson(Object, Type),因为您正在序列化数组列表

更多信息: https://google.github.io/gson/apidocs/com/google/gson/Gson.html

答案 1 :(得分:0)

如果可以选择将ArrayList放到包装器对象中,请不要这样做。

所以Json会成为:

{
    arr: [
        ...
    ]
 }

你的ArrayList可以在一个类中。当然是:

class Wrapper {
    List<Dot>arr = new ArrayList<>();
}

使用gson处理原始列表有点棘手。而且,对我感到羞耻,我不记得了。