PutParcable 时对象丢失数据

我创建了一个用户类并像这样初始化它:


User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");

然后我用用户数据填充对象并将其传递给另一个活动,我这样做是这样的:


Intent intent = new Intent(this, typeof(MyActivity));

Bundle bundlee = new Bundle();

bundlee.PutParcelable("MyUser", MyUser); // Persist user class to next activity

intent.PutExtra("TheBundle", bundlee);

StartActivity(intent);   

然后我在其他活动中检索用户数据,如下所示:


// I initialize a variable again

User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");

MyUser = bundlee.GetParcelable("MyUser") as User;

我得到了除地址、城市和州之外的所有数据。很奇怪......我在代码中放置了断点,当我把它放在包中时,它就在那里,但是当我把它拿出来时,这三个字段是空字符串。


慕侠2389804
浏览 118回答 1
1回答

Helenr

我认为您在这里遇到了一些命名问题。看这部分public User(string Username, string Password, string Firstname, string MiddleInitial, string Lastname, string Suffix,            string address, string city, string state, string Zip, string Zip4, string Homephone, string Cellphone, string Workphone,             string NationalOrgn, string Country, string Company, string Section, string Department, string Usertype,             string Emailaddress)        {            this.mUsername = Username;            this.mPassword = Password;            this.mFirstname = Firstname;            this.mMiddleInitial = MiddleInitial;            this.mLastname = Lastname;            this.mSuffix = Suffix;            this.mAddress = Address;            this.mCity = City;            this.mState = State;            this.mZip = Zip;            this.mZip4 = Zip4;            this.mHomephone = Homephone;            this.mCellphone = Cellphone;            this.mWorkphone = Workphone;            this.mNationalOrgn = NationalOrgn;            this.mCountry = Country;            this.mCompany = Company;            this.mSection = Section;            this.mDepartment = Department;            this.mUsertype = Usertype;            this.mEmailaddress = Emailaddress;        }特别是这个:this.mAddress = Address;this.mCity = City;this.mState = State;正确的方法是:this.mAddress = address;this.mCity = city;this.mState = state;你实际上没有使用你的参数(至少这三个)!这个“错误”有点意思,因为你的编译器没有抱怨。那是因为你实际上是在自己分配你的属性(=null),这是正确的,但肯定不是这里的意图;)!将其更改为上面的应该可以解决问题。
打开App,查看更多内容
随时随地看视频慕课网APP