Android passing an arraylist back to parent activity
Posted
by
Nicklas O
on Stack Overflow
See other posts from Stack Overflow
or by Nicklas O
Published on 2011-03-05T23:29:01Z
Indexed on
2011/03/06
0:10 UTC
Read the original article
Hit count: 271
Hi there. I've been searching for a simple example of this with no luck.
In my android application I have two activities: 1. The main activity which is launched at startup 2. A second activity which is launched by pressing a button on the main activty.
When the second activity is finished (by pressing a button) I want it to send back an ArrayList of type MyObject to the main activity and close itself, which the main activity can then do whatever with it. How would I go about achieving this? I have been trying a few things but it is crashing my application when I start the second activity.
When the user presses button to launch second activity:
Intent i = new Intent(MainActivity.this, secondactivity.class);
startActivityForResult(i, 1);
The array which is bundled back after pressing a button on the second activity:
Intent intent= getIntent();
Bundle b = new Bundle();
b.putParcelableArrayList("myarraylist", mylist);
intent.putExtras(b);
setResult(RESULT_OK, intent);
finish();
And finally a listener on the main activity (although I'm not sure of 100% when this code launches...)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==1){
Bundle extras = data.getExtras();
final ArrayList<MyObject> mylist = extras.getParcelableArrayList("myarraylist");
Toast.makeText(MainActivity.this, mylist.get(0).getName(), Toast.LENGTH_SHORT).show();
}
}
Any ideas where I am going wrong? The onActivityResult() seems to be crashing my application.
EDIT: This is my class MyObject, its called plan and has a name and an id
import android.os.Parcel;
import android.os.Parcelable;
public class Plan implements Parcelable{
private String name;
private String id;
public Plan(){
}
public Plan(String name, String id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String toString(){
return "Plan ID: " + id + " Plan Name: " + name;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(name);
}
public static final Parcelable.Creator<Plan> CREATOR
= new Parcelable.Creator<Plan>() {
public Plan createFromParcel(Parcel in) {
return new Plan();
}
@Override
public Plan[] newArray(int size) {
// TODO Auto-generated method stub
return new Plan[size];
}
};
}
This is my logcat
E/AndroidRuntime( 293): java.lang.RuntimeException: Unable to instantiate activ
ity ComponentInfo{com.daniel.android.groupproject/com.me.android.projec
t.secondactivity}: java.lang.NullPointerException
E/AndroidRuntime( 293): at android.app.ActivityThread.performLaunchActiv
ity(ActivityThread.java:2417)
E/AndroidRuntime( 293): at android.app.ActivityThread.handleLaunchActivi
ty(ActivityThread.java:2512)
E/AndroidRuntime( 293): at android.app.ActivityThread.access$2200(Activi
tyThread.java:119)
E/AndroidRuntime( 293): at android.app.ActivityThread$H.handleMessage(Ac
tivityThread.java:1863)
E/AndroidRuntime( 293): at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 293): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 293): at android.app.ActivityThread.main(ActivityThrea
d.java:4363)
E/AndroidRuntime( 293): at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 293): at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime( 293): at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 293): at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:618)
E/AndroidRuntime( 293): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 293): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 293): at com.daniel.android.groupproject.login.<init>(
login.java:51)
E/AndroidRuntime( 293): at java.lang.Class.newInstanceImpl(Native Method
)
E/AndroidRuntime( 293): at java.lang.Class.newInstance(Class.java:1479)
E/AndroidRuntime( 293): at android.app.Instrumentation.newActivity(Instr
umentation.java:1021)
E/AndroidRuntime( 293): at android.app.ActivityThread.performLaunchActiv
ity(ActivityThread.java:2409)
E/AndroidRuntime( 293): ... 11 more
© Stack Overflow or respective owner