Android的意图putExtra(字符串,序列化)字符串、意图、序列化、Android

由网友(古典仙气的 仙气十足的)分享简介:我很抱歉,如果这个问题已经回答了,我搜索了很多,但我无法找到我的问题的任何问题。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 sorry if this Question is already answered, i searched a lot, but i couldn't find any question with my problem.

我正在写一个Android应用程序从互联网数据库中获取数据。我的第一个活动从数据库中获取数据,我尝试引用传递给整个数据库到另一个活动。

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);

和在此之后,我收到没有解释java.lang.reflect.InvocationTargetException,有什么问题可以。

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

请,如果你知道一种方法来传递一个对象(除INT,字符串...)到另一个活动,帮帮我!

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

推荐答案

您的类服务器要实现接口 Parcelable 为了其目的在于,通过捆传送

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();
     }
 }
阅读全文

相关推荐

最新文章