0

我想将一组对象从Activity 传递MainActivityTheListActivity,但它会生成一个异常并且我的应用程序崩溃。然后我尝试传递一个字符串数组并且效果很好。所以我的问题是当我尝试获取由.MainActivity数组发送的 Extra 时CentreActivityCentreActivity类是Serializable(它实现了这个接口)。这是我的代码MainActivity

for (int i = 0; i < array.length(); i++) {
                JSONObject row = array.getJSONObject(i);
                id[i] = row.getInt("id");
                name[i] = row.getString("identifiant");
               // activities[i] = id[i] + " " + name[i]; //That was just for testing. 
                CentreActivity act = new CentreActivity(id[i], name[i]); 
                listActivityCentre[i] = act; 
            }
            Intent i = new Intent(MainActivity.this, TheList.class); 
            i.putExtra("Activities", listActivityCentre); // Here is my problem 
            startActivity(i); 

这是TheListActivity 的代码:

private CentreActivity listeActivities[] = new CentreActivity[2];

@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listeActivities = (CentreActivity[]) getIntent().getSerializableExtra("Activities");
    for (int i=0; i<listeActivities.length; i++){
        int id = listeActivities[i].getId();
        String iden = listeActivities[i].getIdentifiant(); 
        activities[i] = id + " " + iden; 
    }

    setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, activities));
}

日志猫:

05-14 01:43:28.432: E/AndroidRuntime(3440): FATAL EXCEPTION: main
05-14 01:43:28.432: E/AndroidRuntime(3440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonparameters/com.example.jsonparameters.TheList}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.jsonparameters.CentreActivity[]
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.os.Looper.loop(Looper.java:137)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at java.lang.reflect.Method.invoke(Method.java:511)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at dalvik.system.NativeStart.main(Native Method)
05-14 01:43:28.432: E/AndroidRuntime(3440): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.jsonparameters.CentreActivity[]
05-14 01:43:28.432: E/AndroidRuntime(3440):     at com.example.jsonparameters.TheList.onCreate(TheList.java:18)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.Activity.performCreate(Activity.java:5104)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-14 01:43:28.432: E/AndroidRuntime(3440):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

感谢您帮助我解决这个问题!

4

3 回答 3

2

The exception is coming because you not not accessing correct data what are are putting from previous activity. I guess problem is in listeActivities[] check this and fix your problem , in this case I am using Parse able ArraryList

for sending

 intent.putExtra("LeaveData", lsempleave);

here lsempleave is object of arraylist

for getting

ArrayList<ABC> lsempleave1  = new ArrayList<ABC>();
Bundle extras = getIntent().getExtras();
lsempleave1 = extras.getParcelableArrayList("LeaveData");
于 2013-05-14T12:33:55.150 回答
1

尝试传递一个List<CentreActivity>活动而不是数组。既然List<Type>是孩子Object

于 2013-05-14T12:16:51.997 回答
0

i.putExtra("Activities", listActivityCentre);实际上只需要一个对象而不是一个数组。

http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.io.Serializable%29

于 2013-05-14T07:07:57.897 回答