Опитвам се да използвам XStream за (де) сериализиране на HashMap на някои от моите собствени класове в приложение за Android. Например, един от класовете е Word, който има следните променливи:
private String word;
private boolean capitalizable;
private int useCount;
private HashMap<Character,Integer> endPunctuation;
private HashSet<String> nextWritables;
Вече имам цялата настройка, работеща в стандартно java приложение, просто се опитвам да обвия всичко в Android UI (което работи добре). Сериализиране работи добре в Android. Проблемът, който имам, е, че когато десериализирам, получавам следната грешка:
com.thoughtworks.xstream.converters.ConversionException: Cannot construct chatai.Word as it does not have a no-args constructor : Cannot construct chatai.Word as it does not have a no-args constructor
---- Debugging information ----
message : Cannot construct chatai.Word as it does not have a no-args constructor
cause-exception : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message : Cannot construct chatai.Word as it does not have a no-args constructor
class : java.util.HashMap
required-type : chatai.Word
path : /map/entry/chatai.Word
line number : 1
-------------------------------
Получавам тази грешка, ако използвам която и да е версия на XStream, различна от 1.4.1, в моето настолно приложение. Винаги получавам грешката в приложението си за android, независимо от версията на XStream. Сигурен съм, че настолният има този проблем, защото работи на Java 7. Не съм сигурен за android. Има нещо общо с отражението, поради това предупреждение при добавяне на xstream-1.4.1.jar:
[2011-09-07 21:06:52 - DroidBot] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class
(com.thoughtworks.xstream.XStream$2) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
Един бърз тест ми показва, че сериализирането и десериализирането на String обект в Android работи добре. Как мога да се отърва от тази грешка?