2012-04-10 22 views
1

この質問に既に回答している場合は申し訳ありませんが、私は多くを検索しましたが、私の問題では何の質問も見つかりませんでした。AndroidインテントputExtra(String、Serializable)

インターネットデータベースからデータを取得するアンドロイドアプリを作成しています。私の最初のアクティビティはデータベースからデータを取得し、データベース全体への参照を別のアクティビティに渡そうとします。

それが簡単に簡単に次のようになります。

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

その後、私は別のアクティビティ

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

にこれを渡すためにしようとすると、この後、私はせずにjava.lang.reflect.InvocationTargetExceptionを取得説明、問題は何か。

Object(int、string ...を除く)を別のアクティビティに渡す方法が分かっている場合は、手伝ってください!

+0

スタックトレースを転記できますか? – thedude19

+0

[この記事はあなたを助けるべきである](http://stackoverflow.com/questions/2906925/android-how-do-i-pass-an-object-from-one-activity-to-another) – Chris

+0

はどれでもですリストを実装しているクラスのServerオブジェクトで直列化されたフィールドのうちのどれですか? –

答えて

2

クラスServerは、そのオブジェクトがバンドル経由で転送されるように、インタフェースParcelableを実装する必要があります。

が利用可能hereである、以下の例を参照:

0
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(); 
    } 
} 

Parcelable又はSeralizable interface.Intentが提供する両方のインタフェースを実装しなければならないバンドルを介して渡される必要のあるオブジェクトの場合:

​​

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

しかし、ParcelableはAndroid向けに書かれているため、軽量です。

関連する問題