首页 > 解决方案 > GSON java.lang.IllegalArgumentException:类 android.widget.TextView 声明了多个名为 mMinWidth 的 JSON 字段

问题描述

我正在做什么:我正在尝试使用 GSON 对自定义对象的 ArrayList 进行序列化。我正在制作一个聊天应用程序,所以我需要保存对话。

我已经查看了同一个问题的许多不同解决方案,并且我知道答案.. GSON 正在尝试序列化一个同时在子类和超类中的字段对象,但我的情况有点困难,因为我无权访问这些课程中的任何一个。这是我的堆栈跟踪。

java.lang.IllegalArgumentException: class android.widget.TextView declares multiple JSON fields named mMinWidth
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.Gson.toJson(Gson.java:696)
    at com.google.gson.Gson.toJson(Gson.java:683)
    at com.google.gson.Gson.toJson(Gson.java:638)
    at com.google.gson.Gson.toJson(Gson.java:618)
    at com.widener.ConversationHistoryActivity.saveConversationList(ConversationHistoryActivity.java:93)
    at com.widener.ConversationHistoryActivity$2.onClick(ConversationHistoryActivity.java:81)
    at android.view.View.performClick(View.java:7870)
    at android.widget.TextView.performClick(TextView.java:14951)
    at android.view.View.performClickInternal(View.java:7839)
    at android.view.View.access$3600(View.java:886)
    at android.view.View$PerformClick.run(View.java:29315)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7762)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)

int mMinWidth如您所见,它似乎在 TextView 和 View 中都在抱怨,它是超类。我完全迷失了,我不确定我的代码中包含什么,但我会尽力而为。如果您需要更多信息,请发表评论。

导致问题的代码:

public void saveConversationList()
    {
        SharedPreferences sharedPreferences = getSharedPreferences(SharedPrefStrings.SHARED_PREFS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String jsonOne = gson.toJson(ConversationWrapper.conversationsHistoryList); ERROR ON THIS LINE
        String jsonTwo = gson.toJson(ConversationWrapper.activeConversationIDs);
        editor.putString(SharedPrefStrings.CONVERSATION_LIST, jsonOne);
        editor.putString(SharedPrefStrings.CONVERSATION_LIST_IDS, jsonTwo);
        editor.commit();
    }

会话包装类:

public class ConversationWrapper {
    public static ArrayList<Conversation> conversationsHistoryList;
}

会话课:

public class Conversation extends LinearLayout {

    private TextView id_tag_view;

    private TextView contact_name_view;
    private String contact_name;

    private TextView last_message_view;
    private String last_message;

    private ArrayList<MessageContainer> messageHistory;

    public Conversation(Context context) {
        super(context);
        init();
    }

    public Conversation(Context context, String contact_name, String last_message) {
        super(context);
        this.contact_name = contact_name;
        this.last_message = last_message;
        init();
    }

    public Conversation(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Conversation(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public Conversation(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init()
    {
        inflate(getContext(), R.layout.conversation_preview_view, this);
        this.contact_name_view = (TextView) findViewById(R.id.contact_name_textview);
        this.contact_name_view.setText(this.contact_name);
        this.last_message_view = (TextView) findViewById(R.id.last_message_textview);
        this.last_message_view.setText(this.last_message);
        this.id_tag_view = (TextView) findViewById(R.id.id_tag);
        this.messageHistory = new ArrayList<MessageContainer>();
    }

    public void setContactName(String name)
    {
        this.contact_name = name;
        this.contact_name_view.setText(this.contact_name);
        postInvalidate();
    }

    public void setLastMessage(String lastMessage)
    {
        this.last_message = lastMessage;
        this.last_message_view.setText(this.last_message);
        postInvalidate();
    }

    public void setConversationID(int id)
    {
        this.id_tag_view.setText(id + "");
    }

    public int getConversationID()
    {
        int id = Integer.parseInt(this.id_tag_view.getText().toString());
        return id;
    }

    public void addMessage(MessageContainer mc)
    {
        this.messageHistory.add(mc);
    }

    public ArrayList<MessageContainer> getMessageHistory()
    {
        return this.messageHistory;
    }

}

任何帮助,将不胜感激。谢谢。

标签: javaandroidandroid-studio

解决方案


推荐阅读