无法将childInfo从子活动传递到主活动

时间:2014-01-28 00:31:11

标签: android android-intent android-activity

我的AddBookActivity正在尝试与其父活动进行通信,但我收到此错误,但我看不出它来自哪里。 “无法将结果resultInfo(who = null,request = 1,result = -1,data = Intent {(has extras)}}传递给PARENT ACTIVITY ... NullPointerException”

孩子在崩溃之前调用此方法

    public Book searchBook(){
    /*
     * Search for the specified book.
     */
    // TODO Just build a Book object with the search criteria and return that.

    EditText editText = (EditText) findViewById(R.id.search_title);
    String title = editText.getText().toString();
    editText = (EditText) findViewById(R.id.search_author);
    String author = editText.getText().toString();
    editText = (EditText) findViewById(R.id.search_isbn);
    int isbn = Integer.parseInt(editText.getText().toString());

    Parcel p = Parcel.obtain();
    p.writeInt(isbn);
    p.writeString(title);
    p.writeString(author);
    p.writeString(editText.getText().toString());
    p.writeString("$15.00");
    Intent intent = new Intent();
    Book book = new Book(p);
    p.recycle();
    intent.putExtra(BOOK_RESULT_KEY, book);
    setResult(Activity.RESULT_OK, intent);
    finish();
    return null;
}

}

它在父类

中调用此方法
    protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    // TODO Handle results from the Search and Checkout activities.
    // Use SEARCH_REQUEST and CHECKOUT_REQUEST codes to distinguish the cases.
    // SEARCH: add the book that is returned to the shopping cart.
    if (requestCode == ADD_REQUEST) {
        //See if request was successful
        if (resultCode == RESULT_OK) {
            Bundle b = getIntent().getExtras();
            Book newBook = b.getParcelable("book_result");
            shoppingCart.add(newBook);
            cartsize += 1;
            Toast.makeText(getApplicationContext(), shoppingCart.size(), Toast.LENGTH_SHORT).show();
        }
    }

}

这是Book.class

public class Book implements Parcelable {

// TODO Modify this to implement the Parcelable interface.
// I believe I accomplished the above
// TODO redefine toString() to display book title and price (why?).
// There is no toString() method
public int id;
public String title;
public ArrayList<Author> authors = new ArrayList<Author>();
public int Aflags;
public String isbn;
public String price;

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(id);
    out.writeString(title);
    out.writeTypedList(authors);
    out.writeString(isbn);
    out.writeString(price);
}

public Book(Parcel in) {
    id = in.readInt();
    title = in.readString();
    in.readTypedList(authors, Author.CREATOR);
    isbn = in.readString();
    price = in.readString();
}


public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
    public Book createFromParcel(Parcel in) {
        return new Book(in);
    }
    public Book[] newArray(int size) {
        return new Book[size];
    }
};

@Override
public String toString()
{
    return title + " Price: " + price;
}

}

这是作者类

public class Author implements Parcelable {

// TODO Modify this to implement the Parcelable interface.
// I think I was able to accomplish this
// NOTE: middleInitial may be NULL!

public String firstName;

public String middleInitial;

public String lastName;

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeString(firstName);
    if (middleInitial.length() == 0)
        out.writeString(middleInitial);
    out.writeString(lastName);
}

private Author(Parcel in)
{
    firstName = in.readString();
    if (in.dataSize() == 2)
        middleInitial = in.readString();
    if (in.dataSize() == 1)
        lastName = in.readString();
}

public static final Parcelable.Creator<Author> CREATOR = new Parcelable.Creator<Author>() {
    public Author createFromParcel(Parcel in) {
        return new Author(in);
    }

    public Author[] newArray(int size) {
        return new Author[size];
    }
};

@Override
public int describeContents() {
    return 0;
}

}

1 个答案:

答案 0 :(得分:0)

OnActivityResult方法中,有一个参数Intent intent。使用它。

而不是getIntent()

 Bundle b = getIntent().getExtras(); 

你可以试试这个

Bundle b = intent.getExtras(); //use the Intent param

希望这有帮助!