1

我有这些课程:

public class AllegationContent {

    @DatabaseField(generatedId = true)
    protected long id;

    @DatabaseField(canBeNull = true, foreign = true)
    private AllegationItem allegationItem;

    @DatabaseField()
    protected String content;
    ...
}

和:

public class AllegationItem {
   @DatabaseField(generatedId = true)
    protected long id;

    @ForeignCollectionField(eager = false)
    protected ForeignCollection<AllegationContent> allegationContent;
    ...
}

我怎样才能坚持两者?我正在尝试这个:

    allegationContentDao = getAllegationContentDao();   
    AllegationContent allegationContent = new AllegationContent(); 
    allegationContent.setContent("allegation content");     
    allegationContentDao.create(allegationContent);

    allegationItemDao = getAllegationItemDao();
    AllegationItem allegationItem = new AllegationItem();
    AllegationContent allegationContent2 = allegationContentDao.queryForSameId(allegationContent);  //is that wrong?
    allegationItem.getAllegationContent().add(allegationContent2);
    allegationItemDao.create(allegationItem);

但我收到了这个错误:

04-24 10:41:57.128: 错误/AndroidRuntime(802): java.lang.RuntimeException: 无法启动活动 ComponentInfo{br.com.project/br.com.project.DaoTestActivity}: java.lang.NullPointerException

4

1 回答 1

1

我解决了。该表无法创建,因为没有属性,只有 id(还有什么问题)。NPE 发生是因为 getAllegationContent 返回 null,所以首先我需要创建一个 ForeignCollection 列表,使用 setAllegationContent 设置然后获取。

于 2012-04-25T15:13:26.663 回答