安卓系统BadParcelableException异常:反序列化时ClassNotFoundException异常

7

值被传递并且执行正确,但我在logcat中看到这些被抛出,我想消除这些问题,我查看了旧的论坛,但没有找到具体的解决方法。我在下面发布我的代码,请告诉我为什么会出现这个问题。

public class ExecutionInfo implements Parcelable
{

    private int offSet;

    private List<Integer> pollingIntervalArray = new ArrayList<Integer>();

    private List<String> commandLst= new ArrayList<String>();

    private int repeatCount;

    private int executorId;

    private int processType;


    public ExecutionInfo()
    {

    }

    public ExecutionInfo(Parcel source)
    {
        offSet = source.readInt();
        repeatCount = source.readInt();
        executorId = source.readInt();
        processType = source.readInt();
        source.readStringList(commandLst);
        source.readList(pollingIntervalArray, Integer.class.getClassLoader());
    }

    public int getOffSet()
    {
        return offSet;
    }

    public void setOffSet(int offSet)
    {
        this.offSet = offSet;
    }

    public List<Integer> getInterval()
    {
        return pollingIntervalArray;
    }

    public void setInterval(List<Integer> pollingIntervalVec)
    {
        this.pollingIntervalArray = pollingIntervalVec;
    }

    public List<String> getCommandLst()
    {
        return commandLst;
    }

    public void setCommands(String command)
    {
        commandLst.add(command);
    }

    public int getRepeatCount()
    {
        return repeatCount;
    }

    public void setRepeatCount(int repeatCount)
    {
        this.repeatCount = repeatCount;
    }

    public int getExecutorId()
    {
        return executorId;
    }

    public void setExecutorId(int executorId)
    {
        this.executorId = executorId;
    }

    @Override
    public int describeContents()
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {

          dest.writeInt(offSet);
          dest.writeInt(repeatCount);
          dest.writeInt(executorId);
          dest.writeInt(processType);
          dest.writeStringList(commandLst);
          dest.writeList(pollingIntervalArray);
    }

    public static final Parcelable.Creator<ExecutionInfo> CREATOR = new Parcelable.Creator<ExecutionInfo>()
    {
        public ExecutionInfo createFromParcel(Parcel in)
        {
            return new ExecutionInfo(in);
        }

        public ExecutionInfo[] newArray(int size)
        {
            return new ExecutionInfo[size];
        }
    };

    public int getProcessType()
    {
        return processType;
    }

    public void setProcessType(int processType)
    {
        this.processType = processType;
    }
}

反复抛出的异常是: 但这不会影响执行。

: com.seven.superapptwitter.xmlHandler.ExecutionInfo
07-27 16:52:11.418: WARN/Intent(2458): Failure filling in extras
07-27 16:52:11.418: WARN/Intent(2458): android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.seven.superapptwitter.xmlHandler.ExecutionInfo
07-27 16:52:11.418: WARN/Intent(2458):     at android.os.Parcel.readParcelable(Parcel.java:1883)
07-27 16:52:11.418: WARN/Intent(2458):     at android.os.Parcel.readValue(Parcel.java:1771)
07-27 16:52:11.418: WARN/Intent(2458):     at android.os.Parcel.readMapInternal(Parcel.java:2008)
07-27 16:52:11.418: WARN/Intent(2458):     at android.os.Bundle.unparcel(Bundle.java:208)
07-27 16:52:11.418: WARN/Intent(2458):     at android.os.Bundle.putAll(Bundle.java:281)
07-27 16:52:11.418: WARN/Intent(2458):     at android.content.Intent.fillIn(Intent.java:5127)
07-27 16:52:11.418: WARN/Intent(2458):     at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:195)
07-27 16:52:11.418: WARN/Intent(2458):     at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:177)
07-27 16:52:11.418: WARN/Intent(2458):     at android.app.PendingIntent.send(PendingIntent.java:400)
07-27 16:52:11.418: WARN/Intent(2458):     at com.android.server.AlarmManagerService$AlarmThread.run(AlarmManagerService.java:680)

你能找到不使用序列化的解决方案吗? - bencallis
可能是重复的问题:在使用带有类加载器参数的Parcel.read方法时出现“BadParcelableException: ClassNotFoundException when unmarshalling <myclass>”异常。详见链接:https://dev59.com/ZWMl5IYBdhLWcg3wyJfz - Flow
2个回答

4
我认为这里已经回答了你的问题:Problem unmarshalling parcelables。基本上,因为AlarmManagerService是一个独立的操作系统进程,所以你需要设置类加载器。

你的意思是,这行代码是抛出错误的源头:source.readList(pollingIntervalArray, Integer.class.getClassLoader()); 我没有看到其他需要传递或需要传递类加载器的地方??? - Kavitha
不,你甚至没有到达ClassNotFoundException的那一行(在你提供的堆栈跟踪中)。你并不孤单,试图通过PendingIntent发送可包含对象:https://dev59.com/lXE95IYBdhLWcg3wb9hb#2307764 - Jason
翻译:哎呀...提交得太快了。问题在于AlarmManager没有使用你的类加载器来反序列化PendingIntent,因此它无法访问你的ExecutionInfo类。不幸的是,看起来你可能必须使用一种替代的序列化方法。一种可能性是自己创建一个Parcel(Parcel parcel = Parcel.obtain()),将它序列化(infoObject.writeToParcel(parcel, 0)),获取字节数组(byte[] rawParcel = parcel.marshall())并将该字节数组放入你的bundle中。在另一端,你需要使用Parcel.unmarshall(),然后使用构造函数。 - Jason
是的,我想我需要自己做,实际上值被传递并且它工作得很好,但异常看起来不太好,我想要删除它们,你知道我可以使用哪个链接展示如何手动完成吗?谢谢。 - Kavitha

-7

好的,最终我使用序列化,这样我就不会遇到这个异常了。我将对象序列化并转换为字节数组存储它,然后在PendingIntent检查中传递它。

我确信这会增加开销,但似乎不会影响工作。在这种情况下,当闹钟被触发时,我没有看到任何异常。


1
这并不是一个非常有用的解决方案,因为你可以跳过使用“Parcelable”,而改用“Serializable”。 - Melllvar
1
你不应该依赖于权宜之计,而是要找到一个更好的解决方案。 - Varundroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接