0

When I'm trying to pass an intent from one activity to another my app crashes, without the putExtra() it works fine.

Here's my snippet from the first activity

 ArrayList<Subject> subjectList = new ArrayList<Subject>();

   public void computeGrades(View view){

    Intent intent = new Intent(this, JLCSActivity2B.class);
    intent.putExtra("subjectList", subjectList);
    startActivity(intent);
   }

The Second Activity

public class JLCSActivity2B extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    Intent intent = getIntent();
    ArrayList<Subject> subjectList = (ArrayList<Subject>) intent.getSerializableExtra("subjectList");
 }
}
Zelgh
  • 33
  • 9

3 Answers3

0

Implement your Subject class with Parcelable or Serializable Android does not allow to pass objects between activities unless they implement Parcelable or Serializable. The fact that Parcelable is faster than Serialization makes it a preferred choice of approach while passing an object.

Example implementing Parcelable - http://wptrafficanalyzer.in/blog/android-parcelable-example-passing-data-between-activities/

Parcelable Vs Serializable - http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development

Passiondroid
  • 1,573
  • 1
  • 16
  • 28
0

You need to implement Serializable in your Subject class.

public class Subject implements Serializable {
  //your variables, constructor and getters and setters
}

In your Source Activity, use this :

ArrayList<Subject> subjectList = new ArrayList<Subject>();
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("subjectList", subjectList);

In your Target Activity, use this :

ArrayList<Subject> subjectList = new ArrayList<Subject>();
subjectList = (ArrayList<Question>)getIntent().getSerializableExtra("subjectList");

I hope it helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
0

You can extend your entity class with serializable or better than seriablizable with parcelable interface.

You should implement parcelable interface in your Subject entity

public class Subject implements Parcelable {
    String parceldata;

    // your other fields with getter & setters

    public Subject(Parcel in) {
        String[] data = new String[1];
        in.readStringArray(data);
        this.parceldata = data[0];
    }

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

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

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }
} 

for sending

args.putParcelable("data", subjectlist);

and to access

subjectlist = (ArrayList<Subject>) getArguments().getParcelable("data");

hope this will help you..

paxbat14
  • 101
  • 7