Android Parcel Unmarshalling 未知类型代码

标签 android parcel

好吧,我的问题绝对不同于针对同一问题提出的所有其他问题。

这是我的写法:

public Folder(Parcel in, ClassLoader loader) {
    id = in.readInt();
    persistentId = in.readString();
    folderUri = new FolderUri((Uri) in.readParcelable(loader));
    name = in.readString();
    capabilities = in.readInt();
    // 1 for true, 0 for false.
    hasChildren = in.readInt() == 1;
    syncWindow = in.readInt();
    conversationListUri = in.readParcelable(loader);
    childFoldersListUri = in.readParcelable(loader);
    unseenCount = in.readInt();
    unreadCount = in.readInt();
    totalCount = in.readInt();
    refreshUri = in.readParcelable(loader);
    syncStatus = in.readInt();
    lastSyncResult = in.readInt();
    type = in.readInt();
    iconResId = in.readInt();
    notificationIconResId = in.readInt();
    bgColor = in.readString();
    fgColor = in.readString();
    if (bgColor != null) {
        bgColorInt = Integer.parseInt(bgColor);
    }
    if (fgColor != null) {
        fgColorInt = Integer.parseInt(fgColor);
    }
    loadMoreUri = in.readParcelable(loader);
    hierarchicalDesc = in.readString();
    parent = in.readParcelable(loader);
    lastMessageTimestamp = in.readLong();
    notificationUri = in.readParcelable(loader);
    updatedCountUri = in.readParcelable(loader);
    unreadSenders = in.readString();
}

这是我的写法:

public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(persistentId);
    dest.writeParcelable(folderUri != null ? folderUri.fullUri : null, 0);
    dest.writeString(name);
    dest.writeInt(capabilities);
    // 1 for true, 0 for false.
    dest.writeInt(hasChildren ? 1 : 0);
    dest.writeInt(syncWindow);
    dest.writeParcelable(conversationListUri, 0);
    dest.writeParcelable(childFoldersListUri, 0);
    dest.writeInt(unseenCount);
    dest.writeInt(unreadCount);
    dest.writeInt(totalCount);
    dest.writeParcelable(refreshUri, 0);
    dest.writeInt(syncStatus);
    dest.writeInt(lastSyncResult);
    dest.writeInt(type);
    dest.writeInt(iconResId);
    dest.writeInt(notificationIconResId);
    dest.writeString(bgColor);
    dest.writeString(fgColor);
    dest.writeParcelable(loadMoreUri, 0);
    dest.writeString(hierarchicalDesc);
    dest.writeParcelable(parent, 0);
    dest.writeLong(lastMessageTimestamp);
    dest.writeParcelable(notificationUri, 0);
    dest.writeParcelable(updatedCountUri, 0);
    dest.writeString(unreadSenders);
}

这很完美,没有任何问题。如果我敢于在我的 writeToParcel() 方法中更改任何内容的顺序或添加一个变量并相应地在我从 parcel 方法(即上面的构造函数)读取中进行适当的更改,我会收到上述错误。我真的无法对其或 Android barfs 进行更改。

这就是奇怪的地方。如果我重新启动设备,则不会再崩溃,也不会再出现异常。没有更改代码,只是重新启动。这让我相信 Android 正在缓存我的包裹并完全忽略我正在写给包裹的内容。我进入 Debug模式并在我的 writeToParcel() 方法中设置一个断点,我可以看到它正在执行每一行,但是当它从我的包裹中读取时,它仍然认为顺序是以前的顺序。因此,如果我用 writeString() 交换 writeInt() 的行并在我的构造函数中进行更改,在读取时它仍然认为 Int 应该是 String 所在的位置。卧槽有人请告诉我这是 Android 中的一个已知错误。就像我说的,重启可以解决这个问题。

这里是我做包裹的地方。我基本上是在创建一个在取消通知时触发的 PendingIntent。

final Intent cancelNotificationIntent = new Intent(INTENT_ACTION);
cancelNotificationIntent.putExtra(Utils.EXTRA_FOLDER, folder);
notification.setDeleteIntent(PendingIntent.getService(context, notificationId, cancelNotificationIntent, 0));

这里是我解包的地方:

final Folder folder = intent.getParcelableExtra(Utils.EXTRA_FOLDER);

没什么好看的。帮助?如果有帮助,我将在 Android 4.3 和 4.4 上重现此内容。

最佳答案

好的,原来我的问题是缓存的 PendingIntent。

final Intent cancelNotificationIntent = new Intent(INTENT_ACTION);
cancelNotificationIntent.putExtra(Utils.EXTRA_FOLDER, folder); // This is the parceled object
notification.setDeleteIntent(PendingIntent.getService(context, notificationId, cancelNotificationIntent, 0));

PendingIntent.getService() 在为标志传递 0 时将继续一遍又一遍地使用相同的 PendingIntent 并完全忽略我写入 cancelNotificationIntent 的任何内容。我通过将其更改为来修复它:

notification.setDeleteIntent(PendingIntent.getService(context, notificationId, cancelNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));

我仍然不明白为什么 PendingIntent.getService() 即使在应用升级后(即 adb install -r app.apk)仍继续返回缓存的 Intent。您会认为,如果应用程序已被终止或重新安装,PendingIntent 将会消失,但没有。 Android 会无限期地缓存 PI,直到您重新启动设备。

希望这对其他人有帮助。

关于Android Parcel Unmarshalling 未知类型代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968149/

相关文章:

android - 即使插入耳机,如何通过蓝牙扬声器播放音频?

android - React Native Metro Bundler 没有自动启动

android - 在上传之前调整从画廊或相机拍摄的图像的大小

java - 为什么我的包裹对象返回为空?安卓开发

java - 包裹、类加载器和读取空值

java - 当焦点状态改变时如何改变 "drawableLeft"?

java - 当从 fragment 更改我的 Activity 标题时会导致崩溃

android - 如何在android中将Drawable对象添加到Parcel对象中?

android - 包裹解码未知类型

android - NDK 中的共享内存区域