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

时间:2015-01-02 20:57:22

标签: java android android-activity parse-platform

所以到目前为止我一直做得很好,直到我必须将文本保存到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);
                                }
                            });

                        }

                });


            }
          }

        });

以下是抛出的错误:(这是在我将匹配(&#34;&#34;)更改为等于(&#34;&#34;)后发生的)

01-02 16:20:46.537 31994-31994 / com.ionictech.gamerzio E / AndroidRuntime:FATAL EXCEPTION: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)             at java.lang.reflect.Method.invokeNative(Native Method)             在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(本地方法)

2 个答案:

答案 0 :(得分:0)

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

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

应该说

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

这是Parse保存默认用户对象的方式。除非您有一个标题为&#34; User&#34;的自定义类,否则这将使您的查询始终返回null对象。您可以在此处查看有关此内容的更多信息: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) {

答案 1 :(得分:-1)

使用equals

    if (psnID.equals("")) {

          }

因为

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