2

folks! I wrote a Book class which has Chapter objects (a one to many relationship). It implements the method public List chapters(), as stated in the docs. This is the 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;
}
}

On the main activity I can get the Chapter objects successfully. However, I have to pass a book object to another activity, which has a fragment, and though I get the object's stated attributes (String name and int sort) it throws an exception when I call to chapters():

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();

the thrown exception is the following:

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)

Any ideas what is wrong? Thanks!!!

4

1 回答 1

2

Submitted an issue in Github and now is fixed (https://github.com/melvin7/ActiveAndroid)

On the other hand, putting into the extras bundle only the ID of the column also works. Then I can get the full object with

Model.load(Book.class, id)

于 2013-05-08T19:07:52.837 回答