[英]Need to ignore empty EditText when saving to parse.com
So I've been doing pretty well so far until I have to save the text to parse.com. 因此,到目前为止,我一直做得不错,直到必须将文本保存到parse.com。 The code will not ignore the empty EditText fields which I think is causing the error.
该代码不会忽略我认为引起错误的空白EditText字段。 I have tried to use setText ("") so it will save empty but it still creates an error.
我尝试使用setText(“”),因此它将保存为空,但仍会产生错误。 Does anybody know of a way to ignore the empty EditText fields?
有人知道忽略空的EditText字段的方法吗?
Here is my code below: 这是我的代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.edit_profile);
psnEditText1 = (EditText) findViewById (R.id.psnEditText);
xboxEditText2 = (EditText) findViewById (R.id.xboxEditText);
lolEditText3 = (EditText) findViewById (R.id.lolEditText);
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.whereEqualTo("username", UserId);
query.findInBackground(new FindCallback<ParseObject> () {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for(ParseObject thisUser : objects){
//access the data associated with the ParseUser using the get method
//pu.getString("key") or pu.get("key")
//UNAME = thisUser.getUsername();
PlayStationId = thisUser.getString("PlayStation ID");
XboxLiveID = thisUser.getString("Xbox Live ID");
LeagueofLegendsId = thisUser.getString ("League of Legends ID");
oid = thisUser.getObjectId();
}
psnEditText1.setText (PlayStationId);
xboxEditText2.setText(XboxLiveID);
lolEditText3.setText (LeagueofLegendsId);
} else {
Log.d ("query", "Error: " + e.getMessage ());
}
}
});
saveButton = (Button) findViewById (R.id.saveButton);
saveButton.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
final ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
final String psnID = psnEditText1.getText().toString();
final String xboxID = xboxEditText2.getText().toString();
final String lolID = lolEditText3.getText().toString();
UserId = currentUser.getObjectId ();
ParseQuery<ParseObject> query = ParseQuery.getQuery ("User");
query.whereEqualTo("username", UserId);
// Retrieve the object by id
query.getInBackground (oid, new GetCallback<ParseObject> () {
@Override
public void done(ParseObject UserDataUpdate, ParseException e) {
// if (psnID.isEmpty () || xboxID.isEmpty () ||
//lolID.isEmpty ()) {
// psnEditText1.setText ("Not Available", TextView.BufferType.EDITABLE);
// xboxEditText2.setText ("Not Available", TextView.BufferType.EDITABLE);
//lolEditText3.setText ("Not Available", TextView.BufferType.EDITABLE);
// Toast.makeText (EditProfileActivity.this,
//"Be sure all fields are checked", Toast.LENGTH_LONG).show ();
// } else {
//Add on extra information to current user then save to parse.com
if (psnID.matches ("")){
psnEditText1.setText ("No information entered");
Toast.makeText (EditProfileActivity.this,
"PlayStation ID is empty", Toast.LENGTH_LONG).show ();
} else {
UserDataUpdate.put ("PlayStation ID", psnID);
}
if (xboxID.matches ("")) {
xboxEditText2.setText ("No information entered");
Toast.makeText (EditProfileActivity.this,
"Xbox Live ID is empty",Toast.LENGTH_LONG).show ();
} else {
UserDataUpdate.put ("Xbox Live ID", xboxID);
}
if (lolID.matches ("")) {
lolEditText3.setText ("No information entered");
Toast.makeText (EditProfileActivity.this,
"League of Legends ID is empty",Toast.LENGTH_LONG).show ();
} else {
UserDataUpdate.put ("League of Legends ID", lolID);
}
UserDataUpdate.saveInBackground (new SaveCallback () {
@Override
public void done(ParseException e) {
Toast.makeText (EditProfileActivity.this, "Saved", Toast.LENGTH_LONG).show ();
Intent intent = new Intent (EditProfileActivity.this, MainActivity.class);
startActivity (intent);
}
});
}
});
}
}
});
Here is the error that is being thrown: (this occurred after I changed the matches ("") to equals ("")) 这是抛出的错误:(在我将匹配项(“”)更改为equals(“”)之后发生)
01-02 16:20:46.537 31994-31994/com.ionictech.gamerzio E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com.ionictech.gamerzio.UI.EditProfileActivity$2$1.done(EditProfileActivity.java:141) at com.parse.GetCallback.internalDone(GetCallback.java:43) at com.parse.GetCallback.internalDone(GetCallback.java:29) at com.parse.Parse$6$1.run(Parse.java:975) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) at dalvik.system.NativeStart.main(Native Method) 01-02 16:20:46.537 31994-31994 / com.ionictech.gamerzio E / AndroidRuntime致命异常:com.ionictech.gamerzio.UI.EditProfileActivity $ 2 $ 1.done(EditProfileActivity.java:141中的main java.lang.NullPointerException )com.parse.GetCallback.internalDone(GetCallback.java:29)的com.parse.GetCallback.internalDone(GetCallback.java:43)android.com.parse.Parse的com.parse $ 6 $ 1.run(Parse.java:975) android.os.Looper.loop(Looper.java:137)上的android.os.Handler.dispatchMessage(Handler.java:92)上的.os.Handler.handleCallback(Handler.java:605)android.app.ActivityThread上的。 com.android.internal.os.ZygoteInit $处java.lang.reflect.Method.invoke(Method.java:511)处的main(ActivityThread.java:4424) MethodAndArgsCaller.run(ZygoteInit.java:787)位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)位于dalvik.system.NativeStart.main(本机方法)
Given the fact that UserUpdateData is always null, one main problem here is that you aren't actually querying users. 鉴于UserUpdateData始终为null的事实,这里的一个主要问题是您实际上没有查询用户。 To query users, your line
要查询用户,您的行
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
Should say 应该说
ParseQuery<ParseUser> query = ParseUser.getQuery();
This is how Parse saves the default user object. 这就是Parse保存默认用户对象的方式。 Unless you have a custom class titled "User", this will make your query always return null objects.
除非您有一个名为“ User”的自定义类,否则这将使您的查询始终返回空对象。 You can see more about this here: https://parse.com/docs/android_guide#users-querying
您可以在此处查看有关此内容的更多信息: https : //parse.com/docs/android_guide#users-querying
Also, according to the Parse guide, the lines 另外,根据解析指南,
query.getInBackground (oid, new GetCallback<ParseObject> () {
@Override
public void done(ParseObject UserDataUpdate, ParseException e)
Should be 应该
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
Use equals
使用
equals
if (psnID.equals("")) {
}
Because 因为
"hello".equals(".*e.*"); // false
"hello".matches(".*e.*"); // true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.