gpt4 book ai didi

java - 广播时Android位图包裹解码错误

转载 作者:行者123 更新时间:2023-11-30 01:53:45 26 4
gpt4 key购买 nike

我有一个 Parcelable 联系人对象。我需要将它们放在 ArrayList 中并将其广播到 Activity。在广播监听器中读取 ArrayList 时出错。我尝试了很多来解决这个问题。但找不到任何解决方案来修复它。

错误:

java.lang.RuntimeException: Error receiving broadcast Intent { act=ip. flg=0x10 (has extras) } in io.HomeActivity$1@20d66f72
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:876)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@13cda10b: Unmarshalling unknown type code 7274601 at offset 352
at android.os.Parcel.readValue(Parcel.java:2228)
at android.os.Parcel.readListInternal(Parcel.java:2526)
at android.os.Parcel.readArrayList(Parcel.java:1842)
at android.os.Parcel.readValue(Parcel.java:2173)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelableArrayList(Bundle.java:799)
at android.content.Intent.getParcelableArrayListExtra(Intent.java:5126)
at io.HomeActivity$1.onReceive(HomeActivity.java:149)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            at java.lang.reflect.Method.invoke(Native Method)

联系方式

public class Contact implements Parcelable{

private String id;
private String lookupKey;
private String dispName;
private String email;
private String mobileNo;
private Bitmap photoThumbnail;
private String statusMsg;
private int isRapo;


public Contact(){}

public Contact(String id, String lookupKey, String dispName, String email, String mobileNo, Bitmap photoThumbnail,String statusMsg,int isRapo) {
this.id = id;
this.lookupKey = lookupKey;
this.dispName = dispName;
this.email = email;
this.mobileNo = mobileNo;
//this.photoThumbnail = photoThumbnail;
this.statusMsg = statusMsg;
this.isRapo = isRapo;
}

protected Contact(Parcel in) {
id = in.readString();
lookupKey = in.readString();
dispName = in.readString();
email = in.readString();
mobileNo = in.readString();
//photoThumbnail = in.readParcelable(Bitmap.class.getClassLoader());
//photoThumbnail = (Bitmap)in.readValue(Bitmap.class.getClassLoader());
photoThumbnail = (Bitmap)in.readParcelable(Bitmap.class.getClassLoader());
statusMsg = in.readString();
isRapo = in.readInt();
}

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

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

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getLookupKey() {
return lookupKey;
}

public void setLookupKey(String lookupKey) {
this.lookupKey = lookupKey;
}

public String getDispName() {
return dispName;
}

public void setDispName(String dispName) {
this.dispName = dispName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getMobileNo() {
return mobileNo;
}

public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}

public Bitmap getPhotoThumbnail() {
return photoThumbnail;
}

public void setPhotoThumbnail(Bitmap photoThumbnail) {
this.photoThumbnail = photoThumbnail;
}

public String getStatusMsg() {
return statusMsg;
}

public void setStatusMsg(String statusMsg) {
this.statusMsg = statusMsg;
}

public int getIsRapo() {
return isRapo;
}

public void setIsRapo(int isRapo) {
this.isRapo = isRapo;
}

@Override
public String toString() {
return dispName;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(lookupKey);
dest.writeString(dispName);
dest.writeString(email);
dest.writeString(mobileNo);
//dest.writeParcelable();
/*if(photoThumbnail != null) {
photoThumbnail.writeToParcel(dest, 5);
}*/
if(photoThumbnail != null){
dest.writeParcelable(photoThumbnail,flags);
}
dest.writeString(statusMsg);
dest.writeInt(isRapo);
}
}

服务

contactResIntent.putParcelableArrayListExtra("clist", contactList);

广播监听器

private BroadcastReceiver contactsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(getApplicationContext(),"GOT Contacts response from service!!!",Toast.LENGTH_SHORT).show();
Log.d("@HomeActivity", "GOT Service response.");
/*Bundle data = intent.getExtras();
ArrayList<Parcelable> clist = data.getParcelableArrayList("clist");*/
ArrayList<Contact> clist = intent.getParcelableArrayListExtra("clist");
if(clist != null){
String s = clist.get(0).getClass().toString();
Log.d("@HomeActivity","CONTACTS LIST :"+s+"##"+String.valueOf(clist.size()));
}

最佳答案

首先,您应该避免将位图放入包中。它通常使用大量内存,我经历过一些 TransactionTooLargeException传输大型 parcelables 时(限制为 1Mb)。相反,您可以只编写一个 String 或代表该缩略图的标识符,并在需要时再次加载它。

话虽如此,您可以尝试以下操作(检查是否为空)。

改变构造函数:

protected Contact(Parcel in) {
id = in.readString();
lookupKey = in.readString();
dispName = in.readString();
email = in.readString();
mobileNo = in.readString();
if (in.readByte() == 1) {
photoThumbnail = (Bitmap) in.readParcelable(Bitmap.class.getClassLoader());
}
statusMsg = in.readString();
isRapo = in.readInt();
}

改变writeToParcel

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(lookupKey);
dest.writeString(dispName);
dest.writeString(email);
dest.writeString(mobileNo);
if (photoThumbnail != null){
dest.writeByte((byte) 1);
dest.writeParcelable(photoThumbnail,flags);
} else {
dest.writeByte((byte) 0);
}
dest.writeString(statusMsg);
dest.writeInt(isRapo);
}

关于java - 广播时Android位图包裹解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32587181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com