我有一个包裹式联系对象。我需要将它们放在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()));
}
最佳答案
首先,您应该避免将位图放入包裹中。它通常会占用大量内存,并且在传输大型包裹(限制为1Mb)时遇到了TransactionTooLargeException问题。相反,您可以只写一个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 Bitmap包裹广播时出现编码错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32587181/