使用Parcelable通过Intent传递ArrayList时出现NullPointerException

时间:2014-03-28 05:13:05

标签: android arraylist nullpointerexception parcelable

我正在处理项目并从WCF services获取数据并制作ArrayList,一切正常。我成功地从服务中获取数据。现在我想要的是将 ArrayList通过Intent传递给另一个Activity 。基本上我知道如何发送字符串和int 。值,但我不知道如何通过Intent 传递 ArrayList。我经常搜索并尝试发送ArrayList through Intent Using Parcelable。但现在我收到了 NullPointerException 。有人可以帮助解决我的问题。或者让我知道我是在做正确的还是有其他方法可以做到这一点?在此先感谢

**HOW I TRIED**

Quiz.java

public class Quiz implements Parcelable{

private String id;
private String quiz_code;
private String question;
private String choice_A;
private String choice_B;
private String choice_C;
private String choice_D;
private String ans;
private String quiz_title;


public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getQuiz_code() {
    return quiz_code;
}
public void setQuiz_code(String quiz_code) {
    this.quiz_code = quiz_code;
}
public String getQuestion() {
    return question;
}
public void setQuestion(String question) {
    this.question = question;
}
public String getChoice_A() {
    return choice_A;
}
public void setChoice_A(String choice_A) {
    this.choice_A = choice_A;
}
public String getChoice_B() {
    return choice_B;
}
public void setChoice_B(String choice_B) {
    this.choice_B = choice_B;
}
public String getChoice_C() {
    return choice_C;
}
public void setChoice_C(String choice_C) {
    this.choice_C = choice_C;
}
public String getChoice_D() {
    return choice_D;
}
public void setChoice_D(String choice_D) {
    this.choice_D = choice_D;
}
public String getAns() {
    return ans;
}
public void setAns(String ans) {
    this.ans = ans;
}
public String getQuiz_title() {
    return quiz_title;
}
public void setQuiz_title(String quiz_title) {
    this.quiz_title = quiz_title;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return this.hashCode();
}

public Quiz(){

}

public Quiz(Parcel source) {
    id = source.readString();
    ans = source.readString();
    quiz_title = source.readString();
    choice_A = source.readString();
    choice_B = source.readString();
    choice_C = source.readString();
    choice_D = source.readString();
    quiz_code = source.readString();
    question = source.readString();

}


@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeString(id);
    dest.writeString(ans);
    dest.writeString(quiz_title);
    dest.writeString(quiz_code);
    dest.writeString(question);
    dest.writeString(choice_A);
    dest.writeString(choice_B);
    dest.writeString(choice_C);
    dest.writeString(choice_D);

}

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

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

通过Intent发送ArrayList到anohter Activity:

MainActivity:

in = new Intent(GettingQuizTitleActivity.this,TestActivity.class);

            in.putParcelableArrayListExtra("key", quizList);
            startActivity(in);

我是如何尝试接收ArrayList

SecondActivity:

try{
    Intent intent = null;

    ArrayList<Quiz> test = (ArrayList<Quiz>) intent.<Quiz>getParcelableArrayListExtra("key");

    }
 catch(Exception e){
    e.printStackTrace();
}

4 个答案:

答案 0 :(得分:1)

SecondActivity这一行中,Intentnull

Intent intent = null;

将此更改为

Intent intent = getIntent();

你的Parcelable还有一件事...... 您在writeToParcel()方法中使用Quiz(Parcel source)

中的相同方式编写的内容的顺序

答案 1 :(得分:1)

在你的第二项活动中,

Intent intent = null;  

应该是:

Intent intent = getIntent();  

您正在使用intent(= null)来获取列表。

答案 2 :(得分:1)

将secondActivity的代码更改为此

 Intent intent = getIntent();

ArrayList<Quiz> test = (ArrayList<Quiz>) intent.<Quiz>getParcelableArrayListExtra("key");

答案 3 :(得分:1)

更改此处
  Intent intent = null;

 Intent intent = getIntent();

在您的情况下,您的intent对象为null。所以你有问题。