【问题标题】:IllegalArgumentException multiple json field name [duplicate]IllegalArgumentException 多个 json 字段名称 [重复]
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 即使这样我也会遇到同样的错误。
【解决方案2】:

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

所以 Json 会变成:

{
    arr: [
        ...
    ]
 }

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

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

使用 gson 处理原始列表有点棘手。而且,真可惜,我不记得了。

【讨论】:

  • 还是同样的问题
  • 好的,下一步可能会引入一些更好的 mvc。也许是某个类 DotModel,这只是您的 JSON 的表示。因此,在序列化之前,您必须将 List 映射到 DotModel 列表。顺便说一句,DotModel 类不应有构造函数。 - 或者至少是一个默认构造函数。
猜你喜欢
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2018-08-08
相关资源
最近更新 更多