我正在尝试将对象保存到Parse云并获取其objectId(如果已成功保存)。我正在日志中打印例外,如果有的话。单击“保存”按钮后,日志中不显示ParseException,但返回的objectId始终为null。
这是我的子类:
@ParseClassName("PetOwner")
公共类PetOwner扩展了ParseObject {
public PetOwner(){ super(); }
public String getOwnerId(){
return getString("objectId");
}
public String getOwnerName(){
return getString("name");
}
public ParseGeoPoint getLocation(){
return getParseGeoPoint("location");
}
public String getPhoneNumber(){
return getString("phoneNumber");
}
public Number getRadius(){
return getNumber("searchRadius");
}
public Installation getInstallation(){
return (Installation) getParseObject("installation");
}
public String getBackupEmail(){
return getString("backupemail");
}
public String getEmail(){
return getString("email");
}
public Date createdAt(){
return getDate("createdAt");
}
public Date updatedAt(){
return getDate("updatedAt");
}
public void setOwnerName(String name){ put("name", name); }
public void setPhoneNumber(String phone){ put("phoneNumber", phone); }
public void setEmail(String email){ put("email", email); }
public static ParseQuery<PetOwner> getQuery(){
return ParseQuery.getQuery(PetOwner.class);
}
}
这是我编写的代码,用于在获取用户在EditText视图中输入的名称,电话号码和电子邮件字段的值后将对象保存到云。
FloatingActionButton saveProfile = (FloatingActionButton) findViewById(R.id.save_button);
saveProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (nameChanged && checkPhoneNumber() && checkEmail()) {
final PetOwner petOwner = new PetOwner();
petOwner.setOwnerName(userName.getText().toString());
petOwner.setPhoneNumber(phoneNumber.getText().toString());
petOwner.setEmail(email.getText().toString());
petOwner.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Log.d(TAG, "after saveinbackground is done");
Log.d(TAG, petOwner.getOwnerId() + " is the saved object id");
} else {
Log.d(TAG, "saveinbackground NOT done");
Log.e(TAG, "exception", e);
}
}
});
}
}
});
日志显示:
04-09 19:33:50.385 31818-31818/us.furalert.Furalert D/PetOwnerDetails: after saveinbackground is done
04-09 19:33:50.385 31818-31818/us.furalert.Furalert D/PetOwnerDetails: null is the saved object id
我无法弄清楚我的代码有什么问题。为什么objectId为null?
请帮忙!
答案 0 :(得分:0)
用getObjectId()替换getString(“objectId”),因为ParseObjects有一个内置的方法来检索objectId