【问题标题】:ClassCastException Android, with SerializedObject[]ClassCastException Android,带有 SerializedObject[]
【发布时间】:2014-05-14 10:32:29
【问题描述】:

对不起,标题,

我想通过一个意图传递一个自写类的数组。 这是发件人代码的样子:

private void publishResults(int result, DienstplanDatenreihe[] row, String action)
{
    Intent intent = new Intent(NOTIFICATION);
    intent.putExtra(WorkingCode.TABLE, row);
    intent.putExtra(WorkingCode.RESULT, result);
    intent.putExtra(ACTION,action);
    sendBroadcast(intent);
}

这是接收方代码的样子:

    Bundle extras = intent.getExtras();
    DienstplanDatenreihe[] row =  (DienstplanDatenreihe[]) extras.getSerializable(WorkingCode.TABLE);

这里有一个例外:

Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to HelperCode.DienstplanDatenreihe[]

而且我不知道为什么...互联网上的其他任何人都这样做。 [注意:我的 DienstplanDatenreihe 只包含字符串和字符串[]]

【问题讨论】:

    标签: android android-intent classcastexception serializable


    【解决方案1】:

    DienstplanDatenreihe 可能是一个未实现Parcelable 的自定义类,因此默认情况下Android 无法通过Intent 传递它。你必须让你的类实现Parcelable 接口。

    【讨论】:

    • DienstplanDatenreihe 是一个实现 Serializable 的自定义类,您可以通过 Serializable 或 Parcelable 传递数据,我用 parcelable 解决了同样的错误
    • 在 Android 中强烈建议不要使用 Serializable。请发布您如何实现Parcelable
    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2016-04-12
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多