Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

android - Passing multidimensional array using Serializable

I have passed one multi-dimensional array to another activity using putSerializable() and retrieve using getSerializable. But I got some problems. Please help me to solve my problem..

First activity:

  String [][] selected_list= new String[10][];           
  Bundle list_bundle=new Bundle();
  list_bundle.putSerializable("lists",selected_list);

  Intent list_intent= new Intent(v.getContext(), second_activity.class);
  list_intent.putExtras(list_bundle);
  startActivityForResult(list_intent, 2);

Second Activity:

  String [][] list_new= new String[10][];    
  Bundle b=this.getIntent().getExtras();
  Serializable list= b.getSerializable("list");
  list_new=(String[][])list;   

When I am running my application, my application is suddenly stopped. Is this the right method to retrive the String array? Please give me the solution.. Thank you...


This is the output from Debug window:

CalorieCounter [Android Application]
CalorieCounter [Android Application]
CalorieCounter [Android Application]
CalorieCounter [Android Application]
CalorieCounter [Android Application]
CalorieCounter [Android Application]
CalorieCounter [Android Application]
CalorieCounter [Android Application]
CalorieCounter [Android Application]
DalvikVM[localhost:8605]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626 NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running) Thread [<5> Binder Thread #1] (Running) Thread [<7> Binder Thread #3] (Running)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not use serializable, android implements parcelables in a much much more effective way. The catch is you have to define how parcel the object yourself, but it really isnt that hard.

Simple example:

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

To send it:

Intent list_intent= new Intent(v.getContext(), second_activity.class);
list_intent.putExtras("list",list_bundle);
startActivityForResult(list_intent, 2);

To recieve it:

Bundle b=this.getIntent().getExtras();
MyParcelable list= (MyParcelable)b.getParcelable("list");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...