Android - 应用程序在传递ArrayList时崩溃

时间:2015-08-21 04:11:15

标签: java android arraylist crash

我通过单击按钮将类型为主题的ArrayList传递给另一个活动。但之后应用程序崩溃了。

这是我第一次活动的片段。 ComputeGrades是按下按钮时我正在调用的方法。我没有为显示主题的动态表格输入代码。

public class ACMActivity2A extends Activity {

    ArrayList<Subject> subjectList;

    public class Subject implements Serializable {
        public Subject(){}

        private static final long serialVersionUID = 1L;
        private String courseName;
        private String unit;

        public void setCourseName(String cName){
            this.courseName = cName;
        }    

        public void setUnit(String unit){
            this.unit = unit;
        }

        public String getCourseName(){
            return courseName;
        }

        public String getUnit(){
            return unit;
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        subjectList= new ArrayList<Subject>();
    }

    public void computeGrades(View view){
          Intent intent = new Intent(ACMActivity2A.this, JLCSActivity2B.class);
          Bundle bundle = new Bundle();
          bundle.putSerializable("subjectList", subjectList);
          intent.putExtras(bundle);
          startActivity(intent);
        //Intent.putExtra(SUBJECT_LIST, subjectList);

        //Context ct = getApplicationContext();
        // String text = "working....";
        // int duration = Toast.LENGTH_SHORT;    
        // Toast toast = Toast.makeText(this, text, duration);
        // toast.show();
    }
}

这是第二项活动

    public class JLCSActivity2B extends Activity
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main2);

            Intent intent = this.getIntent();
            Bundle bundle = intent.getExtras();
            ArrayList<Subject> subjectList = (ArrayList<Subject>)bundle.getSerializable("subjectList");
 }
}

这是日志错误:

08-21 12:19:54.688  12490-12490/com.example.activity2a E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.activity2a, PID: 12490
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3846)
            at android.view.View.performClick(View.java:4478)
            at android.view.View$PerformClick.run(View.java:18698)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3841)
            at android.view.View.performClick(View.java:4478)
            at android.view.View$PerformClick.run(View.java:18698)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.activity2a.ACMActivity2A$Subject)
            at android.os.Parcel.writeSerializable(Parcel.java:1323)
            at android.os.Parcel.writeValue(Parcel.java:1271)
            at android.os.Parcel.writeList(Parcel.java:660)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
            at android.os.Bundle.writeToParcel(Bundle.java:1692)
            at android.os.Parcel.writeBundle(Parcel.java:643)
            at android.content.Intent.writeToParcel(Intent.java:7152)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2663)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
            at android.app.Activity.startActivityForResult(Activity.java:3532)
            at android.app.Activity.startActivityForResult(Activity.java:3458)
            at android.app.Activity.startActivity(Activity.java:3780)
            at android.app.Activity.startActivity(Activity.java:3748)
            at com.example.activity2a.ACMActivity2A.computeGrades(ACMActivity2A.java:74)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3841)
            at android.view.View.performClick(View.java:4478)
            at android.view.View$PerformClick.run(View.java:18698)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.io.NotSerializableException: com.example.activity2a.ACMActivity2A
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1366)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1673)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1519)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1483)
            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:981)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1076)
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1406)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1673)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1519)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1483)
            at android.os.Parcel.writeSerializable(Parcel.java:1318)
            at android.os.Parcel.writeValue(Parcel.java:1271)
            at android.os.Parcel.writeList(Parcel.java:660)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
            at android.os.Bundle.writeToParcel(Bundle.java:1692)
            at android.os.Parcel.writeBundle(Parcel.java:643)
            at android.content.Intent.writeToParcel(Intent.java:7152)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2663)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
            at android.app.Activity.startActivityForResult(Activity.java:3532)
            at android.app.Activity.startActivityForResult(Activity.java:3458)
            at android.app.Activity.startActivity(Activity.java:3780)
            at android.app.Activity.startActivity(Activity.java:3748)
            at com.example.activity2a.ACMActivity2A.computeGrades(ACMActivity2A.java:74)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3841)
            at android.view.View.performClick(View.java:4478)
            at android.view.View$PerformClick.run(View.java:18698)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您错误地传递了序列化数据

在这里,您应该如何通过意图传递序列化数据:

Bundle bundle = new Bundle();  
bundle.putSerializable("subject", subjectList);
intent.putExtras(bundle);

并在SecondClass Activity中将其命名为:

Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();

List<Subject> subjects = (List<Subject>)bundle.getSerializable("subject");

您的主题类也缺少序列化ID

private static final long serialVersionUID = 1L;

答案 1 :(得分:0)

public class ACMActivity2A extends Activity {

ArrayList<Subject> subjectList;

public class Subject implements Serializable {
    //---------//
}

public void computeGrades(View view){
      Intent intent = new Intent(ACMActivity2A.this, JLCSActivity2B.class);
      intent .putStringArrayListExtra("Array-List", subjectList)
      startActivity(intent);

  }
}

第二

 public class JLCSActivity2B extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        Intent intent = getIntent();
        ArrayList<String> subjectList =extras.getStringArrayListExtra("Array-List");
   }
 }