Parcelable中的ClassCastException,但也许更深层次的问题?

时间:2013-05-18 13:35:40

标签: android arraylist parcelable

我在一个名为Game

的类中有一个这样的字段
private ArrayList<Question>[] questions;

我是这样写的

public void writeToParcel(Parcel parcel, int flags){
    ...
    parcel.writeArray(questions);
    ...
}

并像这样检索它

private Game(Parcel in) {
    ...
    questions = (ArrayList<Question>[]) in.readArray(Question.class.getClassLoader());
    ...
}

出于某种原因,当我知道活动的onDestroyed()被调用时(当方向从纵向更改为横向,反之亦然)时,它可以正常工作,它可以正常工作并传递所有数据。

但是,当应用程序暂停一段时间(即用户点击主页并继续执行随机操作)时,它会因“无法将Object []强制转换为ArrayList []”而死亡。

logcat的:

05-18 02:52:40.967 I/ActivityManager(401): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.app/.ui.MainActivity} from pid 649
05-18 02:52:51.053 W/ActivityManager(401): Activity idle timeout for ActivityRecord{412389c8 u0 com.example.app/.ui.MainActivity}
05-18 03:00:29.857 I/WindowState(401): WIN DEATH: Window{41238b08 u0 com.example.app/com.example.app.ui.MainActivity}
05-18 03:00:29.865 I/ActivityManager(401): Process com.example.app (pid 29139) has died.
05-18 09:00:53.402 I/ActivityManager(401): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.app/.ui.MainActivity bnds=[373,630][533,830]} from pid 649
05-18 09:00:53.465 I/ActivityManager(401): Start proc com.example.app for activity com.example.app/.ui.MainActivity: pid=9148 uid=10117 gids={50117, 3003, 1028}
05-18 09:00:53.949 E/AndroidRuntime(9148): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.ui.MainActivity}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.util.ArrayList[]
05-18 09:00:53.949 E/AndroidRuntime(9148):  at com.example.app.model.Game.<init>(Game.java:241)
05-18 09:00:53.949 E/AndroidRuntime(9148):  at com.example.app.model.Game.<init>(Game.java:235)
05-18 09:00:53.949 E/AndroidRuntime(9148):  at com.example.app.model.Game$1.createFromParcel(Game.java:228)
05-18 09:00:53.949 E/AndroidRuntime(9148):  at com.example.app.model.Game$1.createFromParcel(Game.java:1)
05-18 09:00:53.949 E/AndroidRuntime(9148):  at com.example.app.ui.MainActivity.onCreate(MainActivity.java:212)
05-18 09:00:53.949 W/ActivityManager(401):   Force finishing activity com.example.app/.ui.MainActivity
05-18 09:00:54.512 W/ActivityManager(401): Activity pause timeout for ActivityRecord{412389c8 u0 com.example.app/.ui.MainActivity}
05-18 09:00:59.981 I/ActivityManager(401): Process com.example.app (pid 9148) has died.

任何人都可以解释发生了什么吗?看起来它释放了我的Activity所占用的内存,因此对象不再存在。这是对的吗?

0 个答案:

没有答案