Android:无法调用没有args的私有android.net.Uri() [英] Android: Failed to invoke private android.net.Uri() with no args

查看:88
本文介绍了Android:无法调用没有args的私有android.net.Uri()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gson将自定义模型的数组列表保存到共享首选项中

I am saving my arraylist of custom model into shared preference using Gson

存储代码:

ArrayList<DownloadProgressDataModel> arrayList = getArrayListFromPref(downloadProgressDataModel);
        SharedPreferences.Editor prefsEditor = getSharedPreferences("APPLICATION_PREF", MODE_PRIVATE).edit();

        Gson gson = new Gson();
        String json = gson.toJson(arrayList);
        prefsEditor.putString("DownloadManagerList", json);
        prefsEditor.apply();
    }

检索

ArrayList<DownloadProgressDataModel> arrayList;
        Gson gson = new Gson();

        SharedPreferences  mPrefs = getSharedPreferences("APPLICATION_PREF", MODE_PRIVATE);
        String json = mPrefs.getString("DownloadManagerList", "");

        if (json.isEmpty()) {
            arrayList = new ArrayList<DownloadProgressDataModel>();
        } else {
            Type uriPath = new TypeToken<ArrayList<DownloadProgressDataModel>>() {
            }.getType();
            arrayList = gson.fromJson(json, uriPath);  <------ Error line
        }

但是在错误行上,我得到: 无法实例化类android.net.Uri

But on the error line I'm getting: can't instantiate class android.net.Uri

模型

public class DownloadProgressDataModel {
    private Uri uriPath;
    private long referenceId;

    public Uri getUriPath() {
        return uriPath;
    }

    public void setUriPath(Uri uriPath) {
        this.uriPath = uriPath;
    }

    public long getReferenceId() {
        return referenceId;
    }

    public void setReferenceId(long referenceId) {
        this.referenceId = referenceId;
    }
}

推荐答案

Uri 类构造函数是私有的,它是一个抽象类.Gson尝试使用Reflection API为 Uri 类创建一个新对象(我们无法为抽象类创建对象).因此,简单的解决方案是将 uriPath 更改为 String ,而不是 Uri .

Uri class constructor is private and it's an abstract class. Gson tries to create a new object for the Uri class using Reflection API(We can't create an object for abstract class). So simple solution is to change the uriPath into String instead of Uri.

 private String uriPath;

这篇关于Android:无法调用没有args的私有android.net.Uri()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆