当前位置: 首页 > 知识库问答 >
问题:

解组Android Intent Parcelable时的RuntimeException

梅逸清
2023-03-14
package org.thoughtcrime.securesms.util;
import android.os.Parcel;
import android.os.Parcelable;
import org.thoughtcrime.securesms.contacts.PlatformContact;
import java.util.ArrayList;
public class ChatIntentParamenters implements Parcelable {
    public static final String FLAG_YES = "Y";
    public static final String FLAG_NO = "N";
    private String title;
    private String additionalTitle;
    private boolean canCreateGroups;
    private boolean canCreateMessages;
    private boolean canDeleteMessages;
    private boolean canInviteFriends;

    private ArrayList<PlatformContact> preloadedContacts;

    public ChatIntentParamenters(String title, String additionalTitle, boolean canCreateGroups, boolean canCreateMessages, boolean canDeleteMessages, boolean canInviteFriends, ArrayList<PlatformContact> preloadedContacts) {

        this.title = title;
        this.additionalTitle = additionalTitle;
        this.canCreateGroups = canCreateGroups;
        this.canCreateMessages = canCreateMessages;
        this.canDeleteMessages = canDeleteMessages;
        this.canInviteFriends = canInviteFriends;

        this.preloadedContacts = preloadedContacts;


    }

    public ChatIntentParamenters(Parcel parcel) {
        this.title = parcel.readString();
        this.additionalTitle = parcel.readString();

        this.canCreateGroups = parcel.readString().equals(FLAG_YES);
        this.canCreateMessages = parcel.readString().equals(FLAG_YES);
        this.canDeleteMessages = parcel.readString().equals(FLAG_YES);
        this.canInviteFriends = parcel.readString().equals(FLAG_YES);

        this.preloadedContacts = parcel.readArrayList(PlatformContact.class.getClassLoader());

    }



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(title);
        dest.writeString(additionalTitle);

        dest.writeString(canCreateGroups?FLAG_YES:FLAG_NO);
        dest.writeString(canCreateMessages?FLAG_YES:FLAG_NO);
        dest.writeString(canDeleteMessages?FLAG_YES:FLAG_NO);
        dest.writeString(canInviteFriends?FLAG_YES:FLAG_NO);

        dest.writeList(preloadedContacts);

    }



    public final static Parcelable.Creator<ChatIntentParamenters> CREATOR = new Parcelable.Creator<ChatIntentParamenters>() {
        @Override
        public ChatIntentParamenters createFromParcel(Parcel source) {
            return new ChatIntentParamenters(source);
        }

        @Override
        public ChatIntentParamenters[] newArray(int size) {
            return new ChatIntentParamenters[size];
        }
    };


    public String getAdditionalTitle() {
        return additionalTitle;
    }

    public void setAdditionalTitle(String additionalTitle) {
        this.additionalTitle = additionalTitle;
    }

    public boolean isCanCreateGroups() {
        return canCreateGroups;
    }

    public void setCanCreateGroups(boolean canCreateGroups) {
        this.canCreateGroups = canCreateGroups;
    }

    public boolean isCanCreateMessages() {
        return canCreateMessages;
    }

    public void setCanCreateMessages(boolean canCreateMessages) {
        this.canCreateMessages = canCreateMessages;
    }

    public boolean isCanDeleteMessages() {
        return canDeleteMessages;
    }

    public void setCanDeleteMessages(boolean canDeleteMessages) {
        this.canDeleteMessages = canDeleteMessages;
    }

    public boolean isCanInviteFriends() {
        return canInviteFriends;
    }

    public void setCanInviteFriends(boolean canInviteFriends) {
        this.canInviteFriends = canInviteFriends;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ArrayList<PlatformContact> getPreloadedContacts() {
        return preloadedContacts;
    }

    public void setPreloadedContacts(ArrayList<PlatformContact> preloadedContacts) {
        this.preloadedContacts = preloadedContacts;
    }
}
package org.thoughtcrime.securesms.contacts;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Classe per passare la lista dei contatti a signal, in modo che non li prenda dai contatti del telefono
 * Created by filippoboatto on 06/09/16.
 */

public class PlatformContact implements Parcelable {
    private long contactId;
    private String firstName;
    private String lastName;
    private String phoneNumber;
    private String imageURI;

    public PlatformContact (long contactId, String firstName, String lastName, String phoneNumber, String imageURI){
        this.contactId = contactId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.imageURI = imageURI;
    }

    public PlatformContact(Parcel parcel) {
        this.contactId = parcel.readInt();
        this.firstName = parcel.readString();
        this.lastName = parcel.readString();
        this.phoneNumber = parcel.readString();
        this.imageURI = parcel.readString();
    }



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(contactId);
        dest.writeString(firstName);
        dest.writeString(lastName);
        dest.writeString(phoneNumber);
        dest.writeString(imageURI);
    }



    public final static Creator<PlatformContact> CREATOR = new Creator<PlatformContact>() {
        @Override
        public PlatformContact createFromParcel(Parcel source) {
            return new PlatformContact(source);
        }

        @Override
        public PlatformContact[] newArray(int size) {
            return new PlatformContact[size];
        }
    };
}
ChatIntentParamenters params = new ChatIntentParamenters(title, additionalTitle, canCreateGroups, canCreateMessages, canDeleteMessages, false, contactsList);
intent.putExtra(ConversationListActivity.INTENT_PARAMS, params);
getActivity().startActivity(intent);

当我在活动中获得意向性额外时,我有一个错误:

ChatIntentParamenters params = intent.getParcelableExtra(INTENT_PARAMS);

共有1个答案

董子航
2023-03-14

尝试替换

this.preloadedContacts = parcel.readArrayList(PlatformContact.class.getClassLoader())

    this.preloadedContacts= new ArrayList<>();
    parcel.readTypedList(this.preloadedContacts, PlatformContact.CREATOR);

而且

    dest.writeList(preloadedContacts);
    dest.writeTypedList(preloadedContacts)
 类似资料:
  • 我使用以下代码通过 JAXB 取消绑定 XML。responseXML 包含从 Web 服务调用返回的 XML 字符串。 以下是取消编组时发生的异常。 有人帮忙解决这个问题。 下面是从XSD自动生成的TestACK类 下面是示例XML

  • 当我从片段< code>HomeFragment运行这段代码时,我得到了这个错误 这里是Need.java 这是 RealmListParcelConverter.java 错误 解组时出现异常:io . realm . needrealmproxy $ $ Parcelable at Android . OS . parcel . read exception(parcel . Java:142

  • 我已经生成java类使用从一个xsd,其中根元素是类型的。 jaxb生成的根元素是 当我尝试解组与该xsd对应的xml并强制转换JaxbElement时,它会引发一个强制转换异常: 片段:

  • 问题内容: 在Go中处理不同时间格式的反序列化的合适方法是什么?在仅接受的RFC 3339中,encoding / json包似乎完全僵化。我可以将其反序列化为字符串,将其转换为RFC 3339,然后再将其取消编组,但我真的不想这样做。有更好的解决方案吗? 问题答案: 您将必须在自定义类型上实现/ 接口,并改用一个示例: 编辑 :添加以检查它是否实际设置,以供将来参考。

  • 编辑:感谢Preetam Kumar,这个问题已经解决,但错误现在在其他地方。当我再次尝试解组我的xml文件时,我收到这个错误,告诉我找不到元素: 然而,我给了xml文件到模式的正确路径,但是它似乎没有使用它...我不明白。 注意:我知道模式和xml标记在下面的示例中不完全匹配,我已经在我的文件中纠正了它。 最近,我不得不使用JAXB java库将一些XML文件解析为java对象。 我制作了一个X

  • 早上好,我试图在我们的代码中找到一个错误,关于当xml没有被格式化时使用JAXB解组XML。我已经做了很多调试和测试,但仍然找不到错误。 xml的解释部分如下所示: 列表最多可包含50.000<代码> 为此,我们有以下代码。解组器的创建被移动到一个接口,以区别我们希望通过jaxb解组的元素,包括子元素(如元素)和那些(如