Question

I'm working on project and getting data from WCF services and make an ArrayList, everything is working fine. I'm successfully getting data from service. Now what I want is Passing that ArrayList through Intent to another Activity. Basically I know how to send string and int. values but I have no idea about how to pass an ArrayList through Intent. I have searched a lot and trying to send ArrayList through Intent Using Parcelable. but now Im recieving a NullPointerException. Can someone help sort out my problem. or let me know does I'm on doing correct or is there any alternate way to do this? Thanks in Advance

**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];
 }
};
}

sending ArrayList to anohter Activity through Intent:

MainActivity:

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

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

how i'm trying to receive an ArrayList

SecondActivity:

try{
    Intent intent = null;

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

    }
 catch(Exception e){
    e.printStackTrace();
}
Was it helpful?

Solution

In your SecondActivity By this line your Intent is null

Intent intent = null;

change this into

Intent intent = getIntent();

That too there is one more thing in your Parcelable.. The order in what what way you have written in your writeToParcel() method you need to follow the same in Quiz(Parcel source)

OTHER TIPS

In your second activity,

Intent intent = null;  

should be:

Intent intent = getIntent();  

You are using intent(=null) to get the list.

change your code of secondActivity to this

 Intent intent = getIntent();

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

Change here from

  Intent intent = null;

to

 Intent intent = getIntent();

in your case your intent object is null. So you got problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top