2014-01-28 3 views
0

저는 Book이라는 Parcelable 객체와 Author라는 객체가 있습니다. Book의 객체 생성자가 작동하지 않는 이유를 알 수 없습니다. 첫 번째 코드는 부모 활동으로 보낼 수 있도록 노력하고 있습니다. 값은 이전에 확인되었고, 나는 책에 매소드로 .toString()를 수행 할 때, 내가 널 가격 얻을 :내 사용자 정의 개체가 왜 구성되지 않습니까?

public class Book implements Parcelable { 

private int id; 
private String title; 
private ArrayList<Author> authors = new ArrayList<Author>(); 
//private int Aflags; 
private String isbn; 
private 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; 
} 

활동 코드

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); 
    System.out.println(book.toString()); 

Book.java 널 (null)을 }

Author.java,

public class Author implements Parcelable { 

// 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; 
} 

은}

+0

Parcel 클래스도 공유 할 수 있습니까? in.readTypedList (작성자, Author.CREATOR)에 가능한 던지기 오류를보고 싶습니다. –

+0

android.os.Parcel에서 가져 왔습니다. 원하는 것은 [여기] (http://developer.android.com/reference/android/os/Parcel.html)입니다. –

답변

-1

안녕하세요 그래서 난 그냥 포기하고 소포를 사용하지 않는 새로운 생성자를했다.

하지만 아래에 쓰여진 내용은 p.setDataPosition (0)입니다. 미래의 사용자에게도 잘 작동 할 수 있습니다.

0
 Parcel p = Parcel.obtain(); 
     p.writeInt(isbn); 
     p.writeString(title); 
     p.writeString(author); // Here i think u need to write list of author and not string 
     p.writeString(editText.getText().toString()); 
     p.writeString("$15.00"); 
     Intent intent = new Intent(); 
     Book book = new Book(p); 
     System.out.println(book.toString()); 

    /*****Edited answer ******/ 
//HERE u go mate this should work, tested code 
    //You need to parse the author text then 

     Parcel p = Parcel.obtain(); 
     p.writeInt(23); // isbn 
     p.writeString("sometitle"); 

     // List<String> data = new List<String>();//parseAuthor(author); // function dependent on what u get 

     Parcel auth = Parcel.obtain(); 
     auth.writeString("firstname"); // firstname 
     auth.writeString("middle"); // middle 
     auth.writeString("lastname"); // lastname 
     auth.setDataCapacity(3); 
     auth.setDataPosition(0); 
     Author a = Author.CREATOR.createFromParcel(auth); 
     ArrayList<Author> authors = new ArrayList<Author>(); 
     authors.add(a); 
     p.writeTypedList(authors); 
     p.writeString("something"); 
     p.writeString("$15.00"); 
     p.setDataPosition(0); 
     Intent intent = new Intent(); 
     Book book = Book.CREATOR.createFromParcel(p); 
     System.out.println(book.toString()); 
+0

저자는 값을 구분하기 위해 공백 이외의 다른 내용이있는 editText 행에서 왔습니다. 그래서 나는 그것을 하나의 문자열로 보냈습니다. 활동에서 더 많은 코드를 추가했습니다. 정확하게 변경하는 방법을 보여 주시겠습니까? 대단히 감사하겠습니다. –

+0

@KevinLourenco 대답 편집 친구 –

+0

나는 p.writeTypedList (authors)에서 nullpointerexception을 얻고 있습니다. –

관련 문제