为了账号安全,请及时绑定邮箱和手机立即绑定

PutParcable 时对象丢失数据

PutParcable 时对象丢失数据

C#
慕侠2389804 2023-05-13 16:06:44
我创建了一个用户类并像这样初始化它:User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");然后我用用户数据填充对象并将其传递给另一个活动,我这样做是这样的:Intent intent = new Intent(this, typeof(MyActivity));Bundle bundlee = new Bundle();bundlee.PutParcelable("MyUser", MyUser); // Persist user class to next activityintent.PutExtra("TheBundle", bundlee);StartActivity(intent);   然后我在其他活动中检索用户数据,如下所示:// I initialize a variable againUser MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");MyUser = bundlee.GetParcelable("MyUser") as User;我得到了除地址、城市和州之外的所有数据。很奇怪......我在代码中放置了断点,当我把它放在包中时,它就在那里,但是当我把它拿出来时,这三个字段是空字符串。
查看完整描述

1 回答

?
Helenr

TA贡献1780条经验 获得超4个赞

我认为您在这里遇到了一些命名问题。看这部分


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),这是正确的,但肯定不是这里的意图;)!


将其更改为上面的应该可以解决问题。


查看完整回答
反对 回复 2023-05-13
  • 1 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信