使用 Serialized 传递多维数组

发布于 2024-09-29 03:06:38 字数 2080 浏览 0 评论 0原文

我已使用 putSerialized() 将一个多维数组传递给另一个活动,并使用 getSerialized 进行检索。但我遇到了一些问题。请帮助我解决我的问题..

第一个活动:

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

第二个活动:

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

当我运行我的应用程序时,我的应用程序突然停止。 这是检索字符串数组的正确方法吗? 请给我解决方案.. 谢谢...


这是调试窗口的输出:

卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
卡路里计数器 [Android 应用程序]
DalvikVM[本地主机:8605]
线程 [<1> main](已挂起(异常 RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) 行:2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) 行:2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) 行:125 ActivityThread$H.handleMessage(Message) 行:2033
ActivityThread$H(Handler).dispatchMessage(Message) 行:99 Looper.loop()行:123 ActivityThread.main(String[]) 行:4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) 行:不可用 [本机方法]
Method.invoke(Object, Object...) 行:521
ZygoteInit$MethodAndArgsCaller.run() 行:868
ZygoteInit.main(String[]) 行:626 NativeStart.main(String[]) 行:不可用 [本机方法]
线程 [<6>活页夹线程#2](正在运行) 线程 [<5>活页夹线程#1](正在运行) 线程 [<7>活页夹线程#3](正在运行)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-06 03:06:38

你不应该使用可序列化,android 以更有效的方式实现 Parcelables。问题是您必须自己定义如何包裹对象,但这实际上并不难。

简单示例:

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

发送:

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

接收:

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文