【问题标题】:Android Parcel Unmarshalling unknown type codeAndroid Parcel Unmarshalling 未知类型代码
【发布时间】:2015-03-10 15:48:59
【问题描述】:

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

这是我的写法:

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 正在缓存我的包裹,并完全忽略了我写给包裹的内容。我进入调试模式并在我的 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 上重现此内容。

【问题讨论】:

  • CREATORs 似乎已被缓存(前 ClassLoader):github.com/android/platform_frameworks_base/blob/master/core/… ...
  • @Selvin 我不敢相信这不是更多人抱怨的更大问题。我也没有办法解决这个问题,你呢?每当我对 CREATOR 进行更改时,我都无法告诉所有人重新启动他们的设备。
  • 问题:这个问题是用简单的 startActivity 的意图出现的还是某种“presist”意图? (我的意思是来自 AlarmManager 的 fx,或者你重新安装应用程序(使用不同的 Parcelable 实现),然后从最近开始一些活动)...... AFAIK 缓存不应该忍受进程杀
  • @Selvin 这个意图进入了一个 PendingIntent。它基本上是在您滑动通知时触发的。当你这样做时,这个 Intent 就会被触发。 notification.setDeleteIntent(PendingIntent.startService(context, notificationId, the_intent, 0));
  • 为意图设置额外的类加载器可能会有所帮助

标签: android parcel


【解决方案1】:

好的,所以我的问题原来是缓存的 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() 即使在应用程序升级后仍继续返回缓存的 Intent(即 adb install -r app.apk)。您会认为如果应用程序已被终止或重新安装,那么 PendingIntent 将会消失,但不会。在您重新启动设备之前,Android 会无限期地缓存 PI。

希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多