Java – Android: cannot call private Android without args net. Uri()

I use gson to save the ArrayList of the custom model to the shared preferences

Storage Code:

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();
    }

retrieval

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
        }

But I got the wrong line: unable to instantiate the class Android net. Uri

Model

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;
    }
}

Solution

The URI class constructor is private and is an abstract class Gson tried to create a new object for URI class using reflection API (we can't create an object for abstract class) Such a simple solution is to change the uripath to string instead of URI

private String uriPath;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>