用户名不被接受?

时间:2018-06-12 03:29:28

标签: java android android-edittext

在下面的代码中,用户名字段显示错误,如图所示,不知道为什么请帮忙?enter image description here

该图片给出了正在接收的错误消息的描述 enter image description here

public class MainActivity extends AppCompatActivity {
EditText text;
ParseUser a;
EditText text2;
public void Onclick(View view)
{
    text=(EditText)findViewById(R.id.editText);
    text2=(EditText)findViewById(R.id.editText2);
    if(view.getId()==R.id.button)
    {
        ParseUser.logInInBackground(text.getText().toString(), text2.getText().toString(), new LogInCallback() {
            @Override
            public void done(ParseUser user, ParseException e) {
                if(user!=null)
                {
                    Toast.makeText(MainActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this,"invalid username or password", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    if(view.getId()==R.id.button2)
    {
     a=new ParseUser();
        a.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(ParseException e) {
                if(e==null)
                {
                    a.setUsername(text.getText().toString());
                    Toast.makeText(MainActivity.this, "user signed in", Toast.LENGTH_SHORT).show();
                    a.setPassword(text2.getText().toString());
                }
                else
                {
                    Toast.makeText(MainActivity.this,"Already user", Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this,e.getMessage().substring(e.getMessage().indexOf(" ")), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
   // ParseAnalytics.trackAppOpenedInBackground(getIntent());
}

1 个答案:

答案 0 :(得分:2)

单击button2后,a.signUpInBackground()会在后台进行检查,发现用户名为空。

您应该在a.signUpinBackground()之前填充用户名和密码,而不是在回调期间(只有在完成注册后才会运行),如http://docs.parseplatform.org/android/guide/#users中所述,示例如下:

ParseUser user = new ParseUser();
user.setUsername("my name"); //in your case, text.getText().toString()
user.setPassword("my pass"); //in your case, text2.getText().toString()  
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
   }
 }
});