使用parse4j,我无法将ParseObject强制转换为扩展ParseObject

时间:2016-01-05 04:45:38

标签: java casting parse4j

使用以下自定义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(),但它仍然不起作用。非常感谢任何帮助。

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

我甚至预装了门列表,希望能解决问题。我很想取消门与作业的关联,只需保存objectId String或自定义UserID。

0 个答案:

没有答案