保存到 parse.com 时需要忽略空的 EditText

Need to ignore empty EditText when saving to parse.com

到目前为止,我一直做得很好,直到我不得不将文本保存到 parse.com。该代码不会忽略我认为导致错误的空 EditText 字段。我曾尝试使用 setText ("") 所以它将保存为空,但它仍然会产生错误。有人知道忽略空 EditText 字段的方法吗?

下面是我的代码:

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

                        }

                });


            }
          }

        });

这是抛出的错误:(这是在我将匹配 ("") 更改为等于 ("") 之后发生的)

01-02 16:20:46.537 31994-31994/com.ionictech.gamerzio E/AndroidRuntime:致命异常:main java.lang.NullPointerException 在 com.ionictech.gamerzio.UI.EditProfileActivity$2$1.done(EditProfileActivity.java:141) 在 com.parse.GetCallback.internalDone(GetCallback.java:43) 在 com.parse.GetCallback.internalDone(GetCallback.java:29) 在 com.parse.Parse$6$1.run(Parse.java:975) 在 android.os.Handler.handleCallback(Handler.java:605) 在 android.os.Handler.dispatchMessage(Handler.java:92) 在 android.os.Looper.loop(Looper.java:137) 在 android.app.ActivityThread.main(ActivityThread.java:4424) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 在 dalvik.system.NativeStart.main(本机方法)

使用equals

    if (psnID.equals("")) {

          }

因为

"hello".equals(".*e.*"); // false
"hello".matches(".*e.*"); // true

鉴于 UserUpdateData 始终为空这一事实,这里的一个主要问题是您实际上并未查询用户。要查询用户,您的行

ParseQuery<ParseObject> query = ParseQuery.getQuery("User");  

应该说

ParseQuery<ParseUser> query = ParseUser.getQuery();  

这就是 Parse 保存默认用户的方式 object。除非您有标题为 "User" 的自定义 class,否则这将使您的查询始终 return null objects。你可以在这里看到更多相关信息:https://parse.com/docs/android_guide#users-querying

此外,根据 Parse 指南,行

query.getInBackground (oid, new GetCallback<ParseObject> () {
  @Override
  public void done(ParseObject UserDataUpdate, ParseException e)

应该是

query.findInBackground(new FindCallback<ParseUser>() {
  public void done(List<ParseUser> objects, ParseException e) {