0

我刚刚发现TinyDB似乎对将对象存储到 SharedPreferences 中非常有用。

我快到了,但我收到一个错误:忘记注册类型适配器? 请注意,Google 的 GSON 库与 TinyDB 一起使用。

所以,让我与您分享我的代码。任何帮助将不胜感激。

这是堆栈跟踪

java.lang.UnsupportedOperationException: Attempted to serialize
java.lang.Class: com.ilepez.weatherapp.data.model.CityArrayList. Forgot to register a type adapter?
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:76)
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:69)
at com.google.gson.Gson.toJson(Gson.java:669)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
at com.google.gson.Gson.toJson(Gson.java:583)
at com.ilepez.weatherapp.utils.TinyDB.putObject(TinyDB.java:462)
at com.ilepez.weatherapp.activity.SearchActivity.onItemClick(SearchActivity.java:67)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:928)
at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:93)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1230)
at android.widget.AdapterView.performItemClick(AdapterView.java:345)
at android.widget.AbsListView.performItemClick(AbsListView.java:1547)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3821)
at android.widget.AbsListView$3.run(AbsListView.java:5841)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    07-17 10:55:26.283 15392-15392/com.ilepez.weatherapp E/libEGL: call to OpenGL ES API with no current context (logged once per thread)

所以,让我与您分享我的代码。任何帮助将不胜感激。

所以这是我将对象添加到 TinyDB 的时刻

City city = new City(description, itemLatitude, itemLongitude);
                ArrayList<Object> savedCity = new ArrayList<>();
                savedCity.add(city);
for (City oCity : CityArrayList.getInstance().getCityList()) {
    savedCity.add(oCity);
}

TinyDB tinydb = new TinyDB(this);
tinydb.putObject("city", CityArrayList.class);

这是我的模型

public class CityArrayList {

    public ArrayList<City> cityList;

    private CityArrayList() {
        cityList = new ArrayList<City>();
    }

    public ArrayList<City> getCityList() {
        return cityList;
    }

    private static CityArrayList instance;

    public static CityArrayList getInstance() {
        if (instance == null) instance = new CityArrayList();
        return instance;
    }
}

而对于城市本身

public class City {

    private String cityName;
    private double cityLat;
    private double cityLong;

    public City(String cityName, double cityLat, double cityLong) {
        this.cityName = cityName;
        this.cityLat = cityLat;
        this.cityLong = cityLong;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public double getCityLat() {
        return cityLat;
    }

    public void setCityLat(double cityLat) {
        this.cityLat = cityLat;
    }

    public double getCityLong() {
        return cityLong;
    }

    public void setCityLong(double cityLong) {
        this.cityLong = cityLong;
    }

    @Override
    public String toString() {
        return "City{" +
                "cityName='" + cityName + '\'' +
                ", cityLat=" + cityLat +
                ", cityLong=" + cityLong +
                '}';
    }
}
4

1 回答 1

3

你传递了错误的东西。该putObject方法要求您传入一个字符串键和一个已经实例化的对象,而不是类类型。

这是您的代码应该正常工作的内容:

City mCity = new City(description, itemLatitude, itemLongitude);
tinydb.putObject("city", mCity);
于 2016-07-18T02:25:06.580 回答