ArrayList的方法“get”上的ClassCastException

时间:2012-08-27 12:18:21

标签: java android arraylist classcastexception

在应用程序控制面板中,我发现了一个错误报告,不仅我无法重现,而且对我来说也毫无意义。

这是堆栈跟踪:

    java.lang.ClassCastException: java.lang.Object[] cannot be cast to ohm.quickdice.entity.RollResult[]
    at ohm.quickdice.activity.QuickDiceActivity.setupRollMenu(QuickDiceActivity.java:575)
    at ohm.quickdice.activity.QuickDiceActivity.onCreateContextMenu(QuickDiceActivity.java:459)
    at android.view.View.createContextMenu(View.java:6229)
    at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:81)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:2292)
    at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:564)
    at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:564)
    at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:564)
    at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:564)
    at android.widget.AbsListView.performLongPress(AbsListView.java:2772)
    at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2717)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4514)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    at dalvik.system.NativeStart.main(Native Method)

这是发生异常的代码:

ArrayList<RollResult[]> resultList;
RollResult[] lastResult;

...

protected void setupRollMenu(int index, ContextMenu menu) {
    RollResult[] rollItem;
    RollResult[] nextItem;
    RollResult mergedRoll;

    if (index < 0) {
        rollItem = lastResult;
    } else {
        rollItem = resultList.get(index);
    }
    if (index + 1 < resultList.size()) {
        nextItem = resultList.get(index + 1); //This is row 575
    } else {
        nextItem = null;
    }
    ...

我想知道指令“nextItem = resultList.get(int);”可以引发此异常,因为resultList定义为ArrayList,nextItem是RollResult []的类型对象...

这可能取决于Android如何将实例状态数据序列化和反序列化为捆绑包?

这里是如何完成序列化的:

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putSerializable(KEY_LAST_ROLL, lastResult);
    outState.putSerializable(KEY_ROLL_LIST, resultList);
    super.onSaveInstanceState(outState);
}

这就是反序列化:

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        Serializable tmp;
        tmp = savedInstanceState.getSerializable(KEY_LAST_ROLL);
        if (tmp instanceof RollResult[]) {
            lastResult = (RollResult[])tmp;
        }
        tmp = savedInstanceState.getSerializable(KEY_ROLL_LIST);
        if (tmp instanceof ArrayList<?>) {
            resultList = (ArrayList<RollResult[]>)tmp;
        }
    }

    if (lastResult == null) {
        lastResult = new RollResult[0];
    }

    if (resultList == null) {
        resultList = new ArrayList<RollResult[]>();
    }
}

感谢。

0 个答案:

没有答案