简体   繁体   中英

java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to Task (my custom ParseObject)

so I have a custom ParseObject called Task and another called Group. I save an arraylist of Tasks in my Group object and later retrieve the Group and Tasks. However, whenever I use group.getList("tasks") I get the error in the title, even though I definitely didn't save a Task as a JSONObject. I've looked on stackoverflow but I can't seem to find anyone else having this problem.

I also have another ParseObject called Message that I'm accessing in the exact same way and I'm not getting this error.

@ParseClassName("Task")
public class Task extends ParseObject {

    public Task getTask(String objectId) {
        return (Task) getParseObject(objectId);
    }

    public String getDescription() {
        return getString("description");
    }

    public String getLocation() {
        return getString("location");
    }

    public String getDate() {
        return getString("date");
    }

    public String getTime() {
        return getString("time");
    }
}

Here is where I retrieve my Group

ParseQuery<Group> query = ParseQuery.getQuery("Group");
query.include("messages");
query.include("tasks");
query.whereEqualTo("name", groupName);
query.getFirstInBackground...

And here's where I try to retrieve the list of Tasks. Changing the Object to Task just moves the error location to the getList line.

ArrayList<Task> tasks = new ArrayList<>();
List<Object> list = this.getList("tasks");
tasks.clear();
for (Object obj : list) {
    tasks.add((Task) obj);  // here is the error spot
}
return tasks;

This is how I save my Task to the Group object

final Task task = new Task();
task.put("description", description);
task.put("date", date);
task.put("time", time);
task.put("location", location);
task.saveInBackground();

group.add("tasks", task);
group.saveInBackground();

I had a similar issue where after getting the list of custom ParseObjects that I stored in a field from my ParseUser, upon trying to access one of the objects, I got a ClassCastException. I investigated further by putting a breakpoint right at the line on which it was crashing, and upon looking at the debugger screen, I noticed that the list of objects that I had retrieved from my ParseUser was actually a list of nulls.

I think the issue might be that the object itself is null and for some reason, when a ParseObject is null, it seems to resolve itself to be a JSONObject rather than a proper ParseObject. I would check your Parse dashboard and make sure that this object is not null.

Also, if your list has pointers to the Task object, make sure that those pointers actually point to Tasks that actually still exist in your database, since it is possible that you have outdated pointers to ghost objects in your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM