我伪装本教程以通过Intent传递自定义对象。
我有2个片段(fragmentA,fragmentB),它们分为2个活动(activityA,activityB)
如果我使用mIntent.putExtra("event","event");
startActivity(mIntent)
工作正常,则该应用会显示正确的页面。
但是当我Parcelable object
时,startActivity(mIntent)
似乎没有任何努力重新启动应用此错误:
03-23 11:49:28.675 1288-1434/system_process E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!
03-23 11:49:28.675 1288-1302/system_process W/WindowManager﹕ view not successfully added to wm, removing view
03-23 11:49:28.675 2311-2311/? D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-23 11:49:28.675 1288-1434/system_process W/ActivityManager﹕ Exception in new application when starting activity com.example.user10.myapplication/.TabbedActivityEvent
android.os.TransactionTooLargeException
at android.os.BinderProxy.transact(Native Method)
at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:750)
at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:967)
at com.android.server.am.ActivityStackSupervisor.attachApplicationLocked(ActivityStackSupervisor.java:385)
at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:5003)
at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:5063)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:394)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2071)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
03-23 11:49:28.675 1288-1434/system_process W/ActivityManager﹕ Force removing ActivityRecord{b142fc90 u0 com.example.user10.myapplication/.TabbedActivityEvent t6}: app died, no saved state
我实现Parcelable的自定义对象的代码:
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
public class EventParcelable implements Parcelable {
private int eventId;
private String title;
private String abstract_;
private String description;
private Bitmap image;
private String referenceFirstName;
private String referenceLastName;
private String referenceEmail;
private String referencePhone;
private String referenceMobile;
private int vote;
private int companyId;
public int getEventId(){
return eventId;
}
public void setEventId(int eventId){
this.eventId = eventId;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getAbstract_(){
return abstract_;
}
public void setAbstract_(String abstract_){
this.abstract_ = abstract_;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public Bitmap getImage(){
return image;
}
public void setImage(Bitmap image){
this.image = image;
}
public String getReferenceFirstName(){
return referenceFirstName;
}
public void setReferenceFirstName(String referenceFirstName){
this.referenceFirstName = referenceFirstName;
}
public String getReferenceLastName(){
return referenceLastName;
}
public void setReferenceLastName(String referenceLastName){
this.referenceLastName = referenceLastName;
}
public String getReferenceEmail(){
return referenceEmail;
}
public void setReferenceEmail(String referenceEmail){
this.referenceEmail = referenceEmail;
}
public String getReferencePhone(){
return referencePhone;
}
public void setReferencePhone(String referencePhone){
this.referencePhone = referencePhone;
}
public String getReferenceMobile(){
return referenceMobile;
}
public void setReferenceMobile(String referenceMobile){
this.referenceMobile = referenceMobile;
}
public int getVote(){
return vote;
}
public void setVote(int vote){
this.vote = vote;
}
public int getCompanyId(){
return companyId;
}
public void setCompanyId(int companyId){
this.companyId = companyId;
}
public static final Creator<Event> CREATOR = new Creator<Event>() {
@Override
public Event createFromParcel(Parcel source) {
Event mEvent = new Event();
mEvent.eventId = source.readInt();
mEvent.title = source.readString();
mEvent.abstract_ = source.readString();
mEvent.description = source.readString();
mEvent.image = source.readParcelable(null);
mEvent.referenceFirstName = source.readString();
mEvent.referenceLastName = source.readString();
mEvent.referenceEmail = source.readString();
mEvent.referencePhone = source.readString();
mEvent.referenceMobile = source.readString();
mEvent.vote = source.readInt();
mEvent.companyId = source.readInt();
return mEvent;
}
@Override
public Event[] newArray(int size) {
return new Event[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.eventId);
dest.writeString(this.title);
dest.writeString(this.abstract_);
dest.writeString(this.description);
dest.writeValue(this.image);
dest.writeString(this.referenceFirstName);
dest.writeString(this.referenceLastName);
dest.writeString(this.referenceEmail);
dest.writeString(this.referencePhone);
dest.writeString(this.referenceMobile);
dest.writeInt(this.vote);
dest.writeInt(this.companyId);
}
}
fragmentA的代码,它位于activityA中以传递数据:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Event event = adapter.getItem(position);
EventParcelable mEventParcelable = new EventParcelable();
mEventParcelable.setEventId(event.eventId);
mEventParcelable.setTitle(event.title);
mEventParcelable.setAbstract_(event.abstract_);
mEventParcelable.setDescription(event.description);
mEventParcelable.setImage(event.image);
mEventParcelable.setReferenceFirstName(event.referenceFirstName);
mEventParcelable.setReferenceLastName(event.referenceLastName);
mEventParcelable.setReferenceEmail(event.referenceEmail);
mEventParcelable.setReferencePhone(event.referencePhone);
mEventParcelable.setReferenceMobile(event.referenceMobile);
mEventParcelable.setVote(event.vote);
mEventParcelable.setCompanyId(event.companyId);
Intent mIntent = new Intent(getActivity(), TabbedActivityEvent.class);
mIntent.putExtra("event", mEventParcelable);
startActivity(mIntent);
}
});
activityB中fragmentB的代码,用于获取传递的数据:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listeventdescription_fragment,container,false);
EventParcelable event1 = (EventParcelable) getActivity().getIntent().getParcelableExtra("event");
Log.d("result",event1.getTitle());
return view;
}
答案 0 :(得分:0)
你应该看看TransactionTooLargeException,它说事务缓冲区的最大大小为1Mb。由于EventParcelable包含Bitmap字段,因此它应该超过最大大小。
如果这个位图来自一个文件,只需将文件路径改为意图中的位图,然后在第二个活动中重新加载位图。否则,您可以尝试使用静态字段(或单例模式)使这个位图在两个活动中都可用。
有关静态变量的示例,请参阅David link。