我想將MainActivity
中的對象數組傳遞給TheList
活動,但它會生成一個異常並導致我的應用程序崩潰。然後我試圖傳遞一個字符串數組,並完美地工作。所以我的問題是,當我嘗試獲得MainActivity
這是一個CentreActivity
數組發送的額外。 CentreActivity
類是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);
這裏是TheList
活動的代碼:
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));
}
的logcat:
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)
謝謝你幫我找出這個問題!
plz還添加日誌與問題 – 2013-05-14 02:24:21
@ρяσѕρєяK它完成了! – R00t 2013-05-14 02:30:49