3

使用以下自定义 parse4j ParseObject 类:

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

    public final String DOOR = "Door";

    public Job() {}

    public setDoor(Door door) {
        put(DOOR, door);
    }

    public Door getDoor() {
        return (Door) get(DOOR); // or (Door) getParseObject(DOOR);
    }
}

@ParseClassName("Door")
public class Door extends ParseObject {
    public Door() {}
}

并加载以下内容:

final static String TAG = "MainWindow";
ArrayList<Job> jobs = new ArrayList<Job>();

ParseQuery<Job> query = ParseQuery.getQuery(Job.class);
query.addAscendingOrder(Job.INDEX);
query.findInBackground(new FindCallback<Job>() {
    @Override
    public void done(List<Job> foundJobs, ParseException e) {
        if(e == null) {
            if(foundJobs == null) {
                System.out.println(TAG + " no jobs found");
            } else {
                for(final Job job : foundJobs) {
                    jobs.add(job);

                    try {
                        job.getParseObject(Job.DOOR).fetchIfNeeded();
                    } catch (ParseException e1) {
                        System.err.println(TAG + " e1: " + e1.getMessage());
                    }

                    System.out.println("objId: " + job.getDoor().getObjectId());
                }
            }
        } else {
            System.out.println(TAG + " e: " + e.getMessage());
        }
    }
});

我收到以下错误:

Exception in thread "pool-1-thread-5" java.lang.ClassCastException: org.parse4j.ParseObject cannot be cast to Door
    at Job.getDoor(Job.java:219)
    at MainWindow$8.done(MainWindow.java:327)
    at org.parse4j.ParseQuery$FindInBackgroundThread.run(ParseQuery.java:596)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

错误发生在System.out.println("objId: " + job.getDoor().getObjectId());

最初我以为是因为我离开了,fetchInBackground()但它仍然无法正常工作。任何帮助是极大的赞赏。

有趣的是,我可以创建一个与 Door 对象链接得很好的新 Job。只是稍后检索它会导致问题。

我什至预先加载了 Doors 列表,希望能解决问题。我很想从作业中取消门的链接,只保存 objectId 字符串或自定义 UserID。

4

0 回答 0