将对象(包含arraylist)从一个活动传递到另一个活动

时间:2014-09-05 13:21:38

标签: android

朋友们,我正在将对象(包含arraylist)从一个活动传递到另一个活动 我通过序列化读取,我们可以在活动中按意图传递对象 但是,当我在最后一个异常细节中提到异常时,这里是我的代码,通过谷歌搜索我读到这个,这是arraylist的问题,我正在通过 但我怎么过......

示例在net上给出,我们只能传递数组列表或paas对象,但是 我的问题是我想传递一个对象,一个数组列表和一些其他变量,目前我只使用单个变量,但它可能更多......

import java.io.Serializable;
import java.util.ArrayList;

public class Common_info implements Serializable{


private static final long serialVersionUID = 1L;



private int numberOfFormats;
private ArrayList<Object> listOfFormats;



public int getNumberOfFormats() {
    return numberOfFormats;
}
public void setNumberOfFormats(int numberOfFormats) {
    this.numberOfFormats = numberOfFormats;
}
public ArrayList<Object> getListOfFormats() {
    return listOfFormats;
}
public void setListOfFormats(ArrayList<Object> listOfFormats) {
    this.listOfFormats = listOfFormats;
}


}

这是我打电话的部分

        Class<?> setClass;              
        Object obj_set_class;

         setClass = Class.forName(str_set_class_name_with_full_path);               
         obj_set_class = setClass.newInstance();



        Object obj_arr_parameters[] = {mode,set_code};
        Class<?> class_arr_Parameters_type[] = new Class[obj_arr_parameters.length];

          for (int i = 0; i < obj_arr_parameters.length; i++) {
                if (obj_arr_parameters[i] instanceof Integer) {

                    class_arr_Parameters_type[i] = Integer.TYPE;

                } else if (obj_arr_parameters[i] instanceof String) {

                    class_arr_Parameters_type[i] = String.class;

                }/*else if (obj_arr_parameters[i] instanceof EditText) {

                    class_arr_Parameters_type[i] = EditText.class;
                }*/

            }
        Method thisMethod = setClass.getDeclaredMethod("getTestInfo", class_arr_Parameters_type);
        Common_info obj_Common_info =   (Common_info) thisMethod.invoke(obj_set_class, obj_arr_parameters);

        System.out.println("number of uis " +obj_Common_info.getNumberOfFormats());

        Intent intent = new Intent(this,ActStartTest2.class);
        intent.putExtra("student_id", student_id);
        intent.putExtra("student_name", student_name);
        intent.putExtra("set_code", set_code);
        intent.putExtra("set_common_info", obj_Common_info);
        //intent.putExtra("mode", CConstants.PRACTICE_MODE);
        startActivity(intent);

EXception Details

    09-05 08:15:56.308: W/System.err(7893): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.cuelearn.cuemath_k8.beans.Common_info)
09-05 08:15:56.318: W/System.err(7893):     at android.os.Parcel.writeSerializable(Parcel.java:1316)
09-05 08:15:56.318: W/System.err(7893):     at android.os.Parcel.writeValue(Parcel.java:1264)
09-05 08:15:56.318: W/System.err(7893):     at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
09-05 08:15:56.318: W/System.err(7893):     at android.os.Bundle.writeToParcel(Bundle.java:1692)
09-05 08:15:56.328: W/System.err(7893):     at android.os.Parcel.writeBundle(Parcel.java:636)
09-05 08:15:56.328: W/System.err(7893):     at android.content.Intent.writeToParcel(Intent.java:7013)
09-05 08:15:56.328: W/System.err(7893):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2076)
09-05 08:15:56.328: W/System.err(7893):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
09-05 08:15:56.328: W/System.err(7893):     at android.app.Activity.startActivityForResult(Activity.java:3424)
09-05 08:15:56.328: W/System.err(7893):     at android.app.Activity.startActivityForResult(Activity.java:3385)
09-05 08:15:56.328: W/System.err(7893):     at android.app.Activity.startActivity(Activity.java:3627)
09-05 08:15:56.328: W/System.err(7893):     at android.app.Activity.startActivity(Activity.java:3595)
09-05 08:15:56.328: W/System.err(7893):     at com.cuelearn.cuemath_k8.act.ActHomeScreen.startTest(ActHomeScreen.java:212)
09-05 08:15:56.338: W/System.err(7893):     at com.cuelearn.cuemath_k8.act.ActHomeScreen.onSelectedConcept(ActHomeScreen.java:140)
09-05 08:15:56.338: W/System.err(7893):     at com.cuelearn.cuemath_k8.ui_files.frag_general.FragConceptsList.onClick(FragConceptsList.java:76)
09-05 08:15:56.338: W/System.err(7893):     at android.view.View.performClick(View.java:4438)
09-05 08:15:56.338: W/System.err(7893):     at android.view.View$PerformClick.run(View.java:18422)
09-05 08:15:56.338: W/System.err(7893):     at android.os.Handler.handleCallback(Handler.java:733)
09-05 08:15:56.338: W/System.err(7893):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-05 08:15:56.338: W/System.err(7893):     at android.os.Looper.loop(Looper.java:136)
09-05 08:15:56.338: W/System.err(7893):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-05 08:15:56.358: W/System.err(7893):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 08:15:56.358: W/System.err(7893):     at java.lang.reflect.Method.invoke(Method.java:515)
09-05 08:15:56.368: W/System.err(7893):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-05 08:15:56.368: W/System.err(7893):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-05 08:15:56.368: W/System.err(7893):     at dalvik.system.NativeStart.main(Native Method)
09-05 08:15:56.378: W/System.err(7893): Caused by: java.io.NotSerializableException: com.cuelearn.cuemath_k8.ui_files.frag_questions.Frag_Ui_Type_1
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-05 08:15:56.378: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-05 08:15:56.398: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-05 08:15:56.398: W/System.err(7893):     at java.util.ArrayList.writeObject(ArrayList.java:648)
09-05 08:15:56.398: W/System.err(7893):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 08:15:56.398: W/System.err(7893):     at java.lang.reflect.Method.invoke(Method.java:515)
09-05 08:15:56.398: W/System.err(7893):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
09-05 08:15:56.408: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
09-05 08:15:56.418: W/System.err(7893):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
09-05 08:15:56.418: W/System.err(7893):     at android.os.Parcel.writeSerializable(Parcel.java:1311)
09-05 08:15:56.418: W/System.err(7893):     ... 25 more

2 个答案:

答案 0 :(得分:0)

Intent intent=new Intent(this,ActStartTest2.class);
intent.putExtra("student_id", student_id);
intent.putExtra("student_name", student_name);
intent.putExtra("set_code", set_code);
intent.putExtra("set_common_info", obj_Common_info);  
startActivity(intent);

在ActStartTest2课程中试试这个

Intent i = getIntent();
Common_info  com_Info = (Common_info)i.getSerializableExtra("set_common_info");

答案 1 :(得分:0)

堆栈跟踪提到java.io.NotSerializableException: com.cuelearn.cuemath_k8.ui_files.frag_questions.Frag_Ui_Type_1 - 这意味着您在要尝试放入Parcel的对象的层次结构中的某个位置引用此(非可序列化)类(可能是obj_common_info,但我不确定)

正如旁注 - 在Android中,建议使用Parcel而不是Serializable类。确实可能需要更多编码,但据说速度更快。检查this