Android - ParseObject Id是空的

时间:2015-02-17 23:16:22

标签: android parse-platform objectid

有谁可以帮我解决这个问题。我有2个活动供用户注册他们的个人资料。我创建了一个ParseObject用户,然后将此用户对象传递给第二个活动。我使用user.getObjectId()来获取用户对象的ID,但ID始终为null。我想知道是否有人可以帮我找到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);
        }
    });
}

3 个答案:

答案 0 :(得分:1)

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);
                }
            }
        });   

答案 1 :(得分:0)

我不确定这是否能解决您的问题,但我认为您需要使用ParseUser与用户合作,而不是ParseObject

答案 2 :(得分:0)

首先使用Parse时的一些一般性评论:

Parse.initialize(this, "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXX");

应该在Application而不是Activity个实例的OnCreate挂钩中发生。

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很有用!