简体   繁体   English

java.lang.ClassCastException:org.json.JSONObject $ 1无法转换为Task(我的自定义ParseObject)

[英]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. 所以我有一个名为Task的自定义ParseObject,另一个名为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. 但是,每当我使用group.getList(“ tasks”)时,即使我绝对没有将Task另存为JSONObject,也都会看到标题错误。 I've looked on stackoverflow but I can't seem to find anyone else having this problem. 我看过stackoverflow,但似乎找不到其他人遇到此问题。

I also have another ParseObject called Message that I'm accessing in the exact same way and I'm not getting this error. 我还有另一个名为Message的ParseObject,我以完全相同的方式访问它,但没有收到此错误。

@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. 将Object更改为Task只会将错误位置移动到getList行。

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. 我遇到了类似的问题,在从ParseUser获取存储在字段中的自定义ParseObjects列表之后,尝试访问其中一个对象时,我得到了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. 我通过在断点所在的行上放置一个断点来进一步研究,然后在查看调试器屏幕时,我注意到从ParseUser中检索到的对象列表实际上是一个空列表。

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. 我认为问题可能在于对象本身为null,并且由于某种原因,当ParseObject为null时,它似乎将自己解析为JSONObject而不是适当的ParseObject。 I would check your Parse dashboard and make sure that this object is not null. 我将检查您的Parse仪表板,并确保该对象不为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. 同样,如果您的列表中有指向Task对象的指针,请确保这些指针实际上指向数据库中实际上仍然存在的Task,因为您可能已经过时了指向列表中幻影对象的指针。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException: java.lang.String 不能转换为 org.json.JSONObject - java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject java.lang.ClassCastException:MapReduce程序中的org.json.JSONObject类 - java.lang.ClassCastException: class org.json.JSONObject in MapReduce program java.lang.ClassCastException:无法将java.lang.String强制转换为net.minidev.json.JSONObject - java.lang.ClassCastException: java.lang.String cannot be cast to net.minidev.json.JSONObject java.lang.ClassCastException: 使用 java 读取 json 文件时,无法将 org.json.simple.JSONArray 转换为 org.json.simple.JSONObject 错误 - java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject error when reading json file iwith java org.json.JSONObject无法转换为java.util.Map - org.json.JSONObject cannot be cast to java.util.Map java.lang.ClassCastException:org.json.simple.JSONArray无法强制转换为org.json.JSONArray - java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray java.lang.ClassCastException: java.util.ArrayList 不能转换为 com.parse.ParseObject - java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.parse.ParseObject org.json.JSONObject无法转换为Serializable_JSONObject - org.json.JSONObject cannot be cast to Serializable_JSONObject java.lang.ClassCastException: 类 net.sf.json.JSONObject 不能转换为类 net.sf.json.JSONArray - java.lang.ClassCastException: class net.sf.json.JSONObject cannot be cast to class net.sf.json.JSONArray java.lang.ClassCastException,DeepNodeListImpl无法强制转换 - java.lang.ClassCastException, DeepNodeListImpl cannot be cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM