Android - ParseObject Id 为空
Android - ParseObject Id is Null
谁能帮我解决这个问题。我有 2 个活动供用户注册他们的个人资料。我创建了一个 ParseObject 用户,然后将这个用户对象传递给第二个 activity。我使用 user.getObjectId() 来获取用户对象的 ID,但 ID 始终为空。我想知道是否有人可以帮助我找到 ParseObject 的用户 ID。请在下面查看我的代码。谢谢
public class RegisterActivity extends Activity {
protected EditText mFirstName;
protected EditText mMiddleName;
protected EditText mLastName;
protected EditText mUserEmail;
protected EditText mPhoneNumber;
protected EditText mAddressLine1;
protected EditText mAddressLine2;
protected EditText mEmergencyName;
protected EditText mEmergencyPhone;
protected EditText mEmergencyRelationship;
protected EditText mEmergencyOtherInfo;
protected Button mNextPageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXX");
//Initialize
mFirstName = (EditText)findViewById(R.id.firstNameRegisterEditText);
mMiddleName = (EditText)findViewById(R.id.middleNameRegisterEditText);
mLastName = (EditText)findViewById(R.id.lastNameRegisterEditText);
mUserEmail = (EditText)findViewById(R.id.emailRegisterEditText);
mPhoneNumber = (EditText)findViewById(R.id.phoneRegisterEditText);
mAddressLine1 = (EditText)findViewById(R.id.addressRegisterText);
mAddressLine2 = (EditText)findViewById(R.id.address2RegisterText);
mEmergencyName = (EditText)findViewById(R.id.emergencyNameEditText);
mEmergencyPhone = (EditText)findViewById(R.id.emergencyPhoneEditText);
mEmergencyRelationship = (EditText)findViewById(R.id.emergencyRelationshipEditText);
mEmergencyOtherInfo = (EditText)findViewById(R.id.medicalInfoEditText);
mNextPageButton = (Button)findViewById(R.id.nextPageButton);
//Listen to register button click
mNextPageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Get first name, middle name, last name, user email, phone number, address line 1, address line 2,
//emergency name, emergency phone, emergency relationship, emergency other info and convert them to string
String firstName = mFirstName.getText().toString().trim();
String middleName = mMiddleName.getText().toString().trim();
String lastName = mLastName.getText().toString().trim();
String userEmail = mUserEmail.getText().toString().trim();
String phoneNumber = mPhoneNumber.getText().toString().trim();
String addressLine1 = mAddressLine1.getText().toString().trim();
String addressLine2 = mAddressLine2.getText().toString().trim();
String emergencyName = mEmergencyName.getText().toString().trim();
String emergencyPhone = mEmergencyPhone.getText().toString().trim();
String emergencyRelationship = mEmergencyRelationship.getText().toString().trim();
String emergencyOtherInfo = mEmergencyOtherInfo.getText().toString().trim();
//String password = mUserPassword.getText().toString().trim();
ParseObject user = new ParseObject("User");
//Store user in parse
user.put("firstName", firstName);
user.put("middleName", middleName);
user.put("lastName", lastName);
user.put("email", userEmail);
user.put("phoneNumber", phoneNumber);
user.put("addressLine1", addressLine1);
user.put("addressLine2", addressLine2);
user.put("emergencyName", emergencyName);
user.put("emergencyPhone", emergencyPhone);
user.put("emergencyRelationship", emergencyRelationship);
user.put("otherInfo", emergencyOtherInfo);
user.put("username", userEmail);
user.saveInBackground();
//Passing user object to AppearanceActivity and take user to appearance register page
Intent intent = new Intent(RegisterActivity.this, AppearanceActivity.class);
intent.putExtra("user_id", user.getObjectId());
startActivity(intent);
}
});
}
我不确定这是否能解决您的问题,但我认为您需要使用 ParseUser
来与用户合作,而不是 ParseObject
。
首先是使用 Parse 时的一些一般性说明:
Parse.initialize(this, "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXX");
应该发生在您 Application
的 OnCreate 挂钩中,而不是 Activity
实例。
ParseObject#saveInBackground 是异步的。因此,它使等待 objectId 更新变得无用。
您可以为保存调用提供回调函数,保存完成后将调用该函数!
尽管如此 我很确定你想 注册 一个用户(因为 RegisterActivity 的名称表明了这一点),所以为什么不你不只是使用这个例子吗:
ParseUser user = new ParseUser();
user.setUsername("my name");
user.setPassword("my pass");
user.setEmail("email@example.com");
// other fields can be set just like with ParseObject
user.put("phone", "650-253-0000");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
在编写可能已经处理好的代码之前,请务必阅读 the official docs。
特别是对于您的用例,您可能会发现 this 有用!
String firstName = mFirstName.getText().toString().trim();
String middleName = mMiddleName.getText().toString().trim();
String lastName = mLastName.getText().toString().trim();
String userEmail = mUserEmail.getText().toString().trim();
String phoneNumber = mPhoneNumber.getText().toString().trim();
String addressLine1 = mAddressLine1.getText().toString().trim();
String addressLine2 = mAddressLine2.getText().toString().trim();
String emergencyName = mEmergencyName.getText().toString().trim();
String emergencyPhone = mEmergencyPhone.getText().toString().trim();
String emergencyRelationship = mEmergencyRelationship.getText().toString().trim();
String emergencyOtherInfo = mEmergencyOtherInfo.getText().toString().trim();
//String password = mUserPassword.getText().toString().trim();
final ParseObject user = new ParseObject("User");
//Store user in parse
user.put("firstName", firstName);
user.put("middleName", middleName);
user.put("lastName", lastName);
user.put("email", userEmail);
user.put("phoneNumber", phoneNumber);
user.put("addressLine1", addressLine1);
user.put("addressLine2", addressLine2);
user.put("emergencyName", emergencyName);
user.put("emergencyPhone", emergencyPhone);
user.put("emergencyRelationship", emergencyRelationship);
user.put("otherInfo", emergencyOtherInfo);
user.put("username", userEmail);
user.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
// Saved successfully.
//Passing user object to AppearanceActivity and take user to appearance register page
Intent intent = new Intent(RegisterActivity.this, AppearanceActivity.class);
intent.putExtra("user_id", user.getObjectId());
startActivity(intent);
} else {
// The save failed.
Log.d("", "User update error: " + e);
}
}
});
谁能帮我解决这个问题。我有 2 个活动供用户注册他们的个人资料。我创建了一个 ParseObject 用户,然后将这个用户对象传递给第二个 activity。我使用 user.getObjectId() 来获取用户对象的 ID,但 ID 始终为空。我想知道是否有人可以帮助我找到 ParseObject 的用户 ID。请在下面查看我的代码。谢谢
public class RegisterActivity extends Activity {
protected EditText mFirstName;
protected EditText mMiddleName;
protected EditText mLastName;
protected EditText mUserEmail;
protected EditText mPhoneNumber;
protected EditText mAddressLine1;
protected EditText mAddressLine2;
protected EditText mEmergencyName;
protected EditText mEmergencyPhone;
protected EditText mEmergencyRelationship;
protected EditText mEmergencyOtherInfo;
protected Button mNextPageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXX");
//Initialize
mFirstName = (EditText)findViewById(R.id.firstNameRegisterEditText);
mMiddleName = (EditText)findViewById(R.id.middleNameRegisterEditText);
mLastName = (EditText)findViewById(R.id.lastNameRegisterEditText);
mUserEmail = (EditText)findViewById(R.id.emailRegisterEditText);
mPhoneNumber = (EditText)findViewById(R.id.phoneRegisterEditText);
mAddressLine1 = (EditText)findViewById(R.id.addressRegisterText);
mAddressLine2 = (EditText)findViewById(R.id.address2RegisterText);
mEmergencyName = (EditText)findViewById(R.id.emergencyNameEditText);
mEmergencyPhone = (EditText)findViewById(R.id.emergencyPhoneEditText);
mEmergencyRelationship = (EditText)findViewById(R.id.emergencyRelationshipEditText);
mEmergencyOtherInfo = (EditText)findViewById(R.id.medicalInfoEditText);
mNextPageButton = (Button)findViewById(R.id.nextPageButton);
//Listen to register button click
mNextPageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Get first name, middle name, last name, user email, phone number, address line 1, address line 2,
//emergency name, emergency phone, emergency relationship, emergency other info and convert them to string
String firstName = mFirstName.getText().toString().trim();
String middleName = mMiddleName.getText().toString().trim();
String lastName = mLastName.getText().toString().trim();
String userEmail = mUserEmail.getText().toString().trim();
String phoneNumber = mPhoneNumber.getText().toString().trim();
String addressLine1 = mAddressLine1.getText().toString().trim();
String addressLine2 = mAddressLine2.getText().toString().trim();
String emergencyName = mEmergencyName.getText().toString().trim();
String emergencyPhone = mEmergencyPhone.getText().toString().trim();
String emergencyRelationship = mEmergencyRelationship.getText().toString().trim();
String emergencyOtherInfo = mEmergencyOtherInfo.getText().toString().trim();
//String password = mUserPassword.getText().toString().trim();
ParseObject user = new ParseObject("User");
//Store user in parse
user.put("firstName", firstName);
user.put("middleName", middleName);
user.put("lastName", lastName);
user.put("email", userEmail);
user.put("phoneNumber", phoneNumber);
user.put("addressLine1", addressLine1);
user.put("addressLine2", addressLine2);
user.put("emergencyName", emergencyName);
user.put("emergencyPhone", emergencyPhone);
user.put("emergencyRelationship", emergencyRelationship);
user.put("otherInfo", emergencyOtherInfo);
user.put("username", userEmail);
user.saveInBackground();
//Passing user object to AppearanceActivity and take user to appearance register page
Intent intent = new Intent(RegisterActivity.this, AppearanceActivity.class);
intent.putExtra("user_id", user.getObjectId());
startActivity(intent);
}
});
}
我不确定这是否能解决您的问题,但我认为您需要使用 ParseUser
来与用户合作,而不是 ParseObject
。
首先是使用 Parse 时的一些一般性说明:
Parse.initialize(this, "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXX");
应该发生在您 Application
的 OnCreate 挂钩中,而不是 Activity
实例。
ParseObject#saveInBackground 是异步的。因此,它使等待 objectId 更新变得无用。 您可以为保存调用提供回调函数,保存完成后将调用该函数!
尽管如此 我很确定你想 注册 一个用户(因为 RegisterActivity 的名称表明了这一点),所以为什么不你不只是使用这个例子吗:
ParseUser user = new ParseUser();
user.setUsername("my name");
user.setPassword("my pass");
user.setEmail("email@example.com");
// other fields can be set just like with ParseObject
user.put("phone", "650-253-0000");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
在编写可能已经处理好的代码之前,请务必阅读 the official docs。 特别是对于您的用例,您可能会发现 this 有用!
String firstName = mFirstName.getText().toString().trim();
String middleName = mMiddleName.getText().toString().trim();
String lastName = mLastName.getText().toString().trim();
String userEmail = mUserEmail.getText().toString().trim();
String phoneNumber = mPhoneNumber.getText().toString().trim();
String addressLine1 = mAddressLine1.getText().toString().trim();
String addressLine2 = mAddressLine2.getText().toString().trim();
String emergencyName = mEmergencyName.getText().toString().trim();
String emergencyPhone = mEmergencyPhone.getText().toString().trim();
String emergencyRelationship = mEmergencyRelationship.getText().toString().trim();
String emergencyOtherInfo = mEmergencyOtherInfo.getText().toString().trim();
//String password = mUserPassword.getText().toString().trim();
final ParseObject user = new ParseObject("User");
//Store user in parse
user.put("firstName", firstName);
user.put("middleName", middleName);
user.put("lastName", lastName);
user.put("email", userEmail);
user.put("phoneNumber", phoneNumber);
user.put("addressLine1", addressLine1);
user.put("addressLine2", addressLine2);
user.put("emergencyName", emergencyName);
user.put("emergencyPhone", emergencyPhone);
user.put("emergencyRelationship", emergencyRelationship);
user.put("otherInfo", emergencyOtherInfo);
user.put("username", userEmail);
user.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
// Saved successfully.
//Passing user object to AppearanceActivity and take user to appearance register page
Intent intent = new Intent(RegisterActivity.this, AppearanceActivity.class);
intent.putExtra("user_id", user.getObjectId());
startActivity(intent);
} else {
// The save failed.
Log.d("", "User update error: " + e);
}
}
});