人们!我写了一个Book类,它有Chapter对象(一对多的关系)。它实现了方法public List章节(),如文档中所述。这是Book.java
@Table(name = "Books")
public class Book extends Model implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "Name", unique = true, onUniqueConflict = Column.ConflictAction.IGNORE)
public String name;
@Column(name = "Sort")
public int sort;
public Book() {
super();
}
public Book(String name, int sort) {
super();
this.name = name;
this.sort = sort;
}
public List<Chapter> chapters() {
return getMany(Chapter.class, "Book");
}
@Override
public String toString() {
return name;
}
}
在主要活动上,我可以成功获得章节对象。但是,我必须将一个book对象传递给另一个有一个片段的活动,虽然我得到了对象的声明属性(String name和int sort),但当我调用chapter()时它会引发异常:
Bundle bundle = getIntent().getExtras();
Book book = (Book) bundle.getSerializable("BOOK");
// This line is executed successfully
Log.d("TAGGED", "Recovered book: " + book.name + " has " + book.sort + " as its sort");
// This is the line that throws an exception
ArrayList<Chapter> chapters = book.chapters();
抛出的异常如下:
05-06 15:21:59.701: E/AndroidRuntime(9647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hanovit.libraria/com.hanovit.libraria.chapter.ChapterActivity}: java.lang.NullPointerException
05-06 15:21:59.701: E/AndroidRuntime(9647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-06 15:21:59.701: E/AndroidRuntime(9647): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-06 15:21:59.701: E/AndroidRuntime(9647): Caused by: java.lang.NullPointerException
05-06 15:21:59.701: E/AndroidRuntime(9647): at com.activeandroid.query.From.getArguments(From.java:207)
05-06 15:21:59.701: E/AndroidRuntime(9647): at com.activeandroid.query.From.execute(From.java:183)
05-06 15:21:59.701: E/AndroidRuntime(9647): at com.activeandroid.Model.getMany(Model.java:266)
任何想法有什么不对?感谢!!!
答案 0 :(得分:2)
在Github中提交了一个问题,现在已修复(https://github.com/melvin7/ActiveAndroid)
另一方面,只添加列的ID也可以使用附加组件。然后我可以使用
Model.load(Book.class, id)
获得完整的对象