1

I'm sorry if this Question is already answered, i searched a lot, but i couldn't find any question with my problem.

I'm writing an android app which gets data from an internet database. My first activity retrieves the data from the database, and i try to pass a reference to the whole database to another activity.

it briefly looks briefly like this:

//server is wrapper class for my database connection/ data retrieving
Server server = new Server(...connection data...);
server.connect();
server.filldata();

And after that i try to pass this to another activity

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("server", server); //server, and all implements Serializable
startActivity(intent);

And after this i get a java.lang.reflect.InvocationTargetException without explanation, what the problem could be.

Please if you know a way to pass an Object (except for int, string...) to another activity, help me!

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • Can you post your stacktrace? – thedude19 Apr 10 '12 at 18:42
  • [This post should help you out some](http://stackoverflow.com/questions/2906925/android-how-do-i-pass-an-object-from-one-activity-to-another) – Chris Apr 10 '12 at 18:42
  • are any of the fields serialized with the Server object of a class that implements List? – Chris Bye Apr 10 '12 at 18:43
  • I dont know if i'm doing it right, (i started to code in eclipse today, until now i worked in Netbeans), but here are the error messages: http://pastebin.com/cRNHtp8G – Balázs Édes Apr 10 '12 at 18:47
  • @bali182 : InvocationException occurs when somethin else is wrong. I see line com.dbtest.MainScreen$2.onClick(MainScreen.java:41) 41. Check it or paste the complete code – Akhil Apr 10 '12 at 19:12
  • That is the point when i put the object in the intent, and try to start the other activity. Intent intent = new Intent(MainScreen.this, ClientList.class); intent.putExtra("server", server); startActivityForResult(intent, 0); its in an onclicklistener – Balázs Édes Apr 10 '12 at 19:27
  • Ok, i found the problem. In my Server class i use JDBC drivers. They are in a jar file, so i cant see the source, but i dont think, that they implement the Serializable interface. If i only pass the relevant data (an arraylist), there is no problem. So i think i was trying to do this too lazy... – Balázs Édes Apr 10 '12 at 19:36

2 Answers2

2

Your class Server should implement interface Parcelable in order for its object to be transferred via bundle.

See the example below, which is available here:

 public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

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

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

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

For any object which needs to be passed via Bundle must implement the Parcelable or Seralizable interface.Intent provides both the interfaces:

putExtra(String name, Parcelable value)
putExtra(String name, Serializable value)

https://developer.android.com/reference/android/content/Intent.html

But it is advisable to use Parcelable as it is written specifically for Android and is light-weight in nature.

Shinoo Goyal
  • 601
  • 8
  • 10