通过传递序列化的多维数组 [英] Passing multidimensional array using Serializable

查看:103
本文介绍了通过传递序列化的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已用putSerializable()传递的一个多维阵列到另一个活动,并使用getSerializable检索。但我得到了一些问题。请帮我解决我的问题。

第一项活动:

 的String [] [] = selected_list新的String [10] [];
  捆绑list_bundle =新包();
  list_bundle.putSerializable(名单,selected_list);  意图list_intent =新意图(v.getContext(),second_activity.class);
  list_intent.putExtras(list_bundle);
  startActivityForResult(list_intent,2);

第二项活动:

 的String [] [] = list_new新的String [10] [];
  束B = this.getIntent()getExtras()。
  序列化列表= b.getSerializable(名单);
  list_new =(字符串[] [])名单;

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


这是从调试窗口输出:


  

CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
  CalorieCounter [Android应用程序]结果
      DalvikVM [本地主机:8605]结果
          螺纹[< 1>主](暂停(例外的RuntimeException))结果
              ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,意图)行:2663结果
              ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,意图)行:2679结果
              ActivityThread.access $ 2300(ActivityThread,ActivityThread $ ActivityRecord,意图)线:125
              ActivityThread $ H.handleMessage(消息)行:2033结果
              ActivityThread $ H(处理器).dispatchMessage(消息)行:99
              Looper.loop()行:123
              ActivityThread.main(字符串[])行:4627结果
              Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本机方法]结果
              Method.invoke(对象,对象...)线:521结果
              ZygoteInit $ MethodAndArgsCaller.run()行:868结果
              ZygoteInit.main(字符串[])线:626
              NativeStart.main(字符串[])行:不可用[本机方法]结果
          螺纹[6;>捆扎线#2](运行)
          螺纹[小于5>捆扎线#1](运行)
          螺纹[7;>捆扎线#3](运行)



解决方案

您不应该在更更有效的方式使用序列化的,Android的工具Parcelables并。美中不足的是,你必须定义自己如何包裹物,但它真正地不是那么难。

简单的例子:

 公共类MyParcelable实现Parcelable {
     私人诠释MDATA;     公众诠释describeContents(){
         返回0;
     }     公共无效writeToParcel(包裹出来,诠释标志){
         out.writeInt(MDATA);
     }     公共静态最终Parcelable.Creator< MyParcelable> CREATOR
             =新Parcelable.Creator< MyParcelable>(){
         公共MyParcelable createFromParcel(包裹中){
             返回新MyParcelable(中);
         }         公共MyParcelable [] newArray(INT大小){
             返回新MyParcelable【尺寸】;
         }
     };     私人MyParcelable(包裹中){
         MDATA = in.readInt();
     }
 }

要发送:

 意图list_intent =新意图(v.getContext(),second_activity.class);
list_intent.putExtras(名单,list_bundle);
startActivityForResult(list_intent,2);

要接受她:

 叠B = this.getIntent()getExtras()。
MyParcelable名单=(MyParcelable)b.getParcelable(名单);

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)

解决方案

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

这篇关于通过传递序列化的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆