2013-05-14 114 views
0

我想將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) 

謝謝你幫我找出這個問題!

+0

plz還添加日誌與問題 – 2013-05-14 02:24:21

+0

@ρяσѕρєяK它完成了! – R00t 2013-05-14 02:30:49

回答

1

嘗試通過陣列的List<CentreActivity>活性代替。由於List<Type>Object的子女

+1

更具體地說,可能會首選'ArrayList'。 – adrianp 2013-05-14 12:21:40

+0

爲true,但實現List接口的任何對象都可以完成這項工作。 – Ercan 2013-05-16 08:14:09

2

因爲您沒有訪問正確的數據,所以前面的活動會引起異常。我想問題出在listeActivities[]檢查並解決您的問題,在這種情況下,我使用的解析能夠ArraryList

發送

intent.putExtra("LeaveData", lsempleave); 

這裏lsempleave是ArrayList的

對象爲獲得

ArrayList<ABC> lsempleave1 = new ArrayList<ABC>(); 
Bundle extras = getIntent().getExtras(); 
lsempleave1 = extras.getParcelableArrayList("LeaveData"); 
相關問題