将数据添加到Parse.com Android

时间:2015-04-18 18:07:39

标签: android database parse-platform

我有两个活动,第一个是注册活动,第二个是我想在第一个活动中将一些数据添加到同一个数据库中。 单击RegisterInformationActivity中的btn时出现的错误是: “在注册之前无法保存parseuser”

第一项活动:

public class RegisterActivity extends Activity {

ImageButton signup;
EditText username;
EditText password;
EditText email;
EditText confirmpassword;
String usernametxt;
String emailtxt;
String passwordtxt;
String confirmpasswordtxt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_singup);

    username = (EditText)findViewById(R.id.editTextFirstNameSingup);
    password = (EditText)findViewById(R.id.editTextPasswordSingup);
    email = (EditText)findViewById(R.id.editTextEmailSingup);
    confirmpassword = (EditText)findViewById(R.id.editTextPasswordConfirmSingup);

    signup = (ImageButton) findViewById(R.id.imageButtonSignUpWithEmail);

    signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            usernametxt = username.getText().toString();
            emailtxt = email.getText().toString();
            passwordtxt = password.getText().toString();
            confirmpasswordtxt = confirmpassword.getText().toString();

            if(usernametxt.equals("") && passwordtxt.equals("") && emailtxt.equals("") && confirmpasswordtxt.equals("")) {
                Toast.makeText(getApplicationContext(), "Please complete the sign up form", Toast.LENGTH_LONG).show();
            }else if(!confirmpasswordtxt.equals(passwordtxt)) {
                Toast.makeText(getApplicationContext(), "Passwords doesn't match", Toast.LENGTH_LONG).show();
            }else{
                // Save new user data into Parse.com storage data (VALJDA!)
                ParseUser user = new ParseUser();
                user.setUsername(usernametxt);
                user.setPassword(passwordtxt);
                user.setEmail(emailtxt);
                user.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {
                        if(e == null){
                            // Toast for succefull registration
                            Intent intent = new Intent(RegisterActivity.this, RegisterInformationActivity.class);
                            startActivity(intent);
                            finish();
                            Toast.makeText(getApplicationContext(), "Successfully Signed up, please log in.", Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(getApplicationContext(), "Sign up Error", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }


        }
    });

}}

第二项活动,RegisterInfromationActivity:

public class RegisterInformationActivity extends Activity {

ImageButton btn;
EditText cigsInADay;
EditText boxWorth;
EditText cigsInABox;
EditText howLong;
String cigsInADayTxt;
String boxWorthTxt;
String cigsInABoxTxt;
String howLongTxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_registerinformation);

    cigsInABox = (EditText)findViewById(R.id.editTextCigginabox);
    boxWorth = (EditText)findViewById(R.id.editTextBoxworth);
    cigsInADay = (EditText)findViewById(R.id.editTextCigarettes);
    howLong = (EditText)findViewById(R.id.editTextHowlong);

    btn = (ImageButton)findViewById(R.id.imageButtonToWelcomeScreen);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            cigsInABoxTxt = cigsInABox.getText().toString();
            boxWorthTxt = boxWorth.getText().toString();
            cigsInADayTxt = cigsInADay.getText().toString();
            howLongTxt = howLong.getText().toString();

            if(cigsInABoxTxt.equals("") && boxWorthTxt.equals("") && cigsInADayTxt.equals("") && howLongTxt.equals("")){
                Toast.makeText(getApplicationContext(),"Please complete all forms",Toast.LENGTH_LONG).show();
            }else{

                if(ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
                    // If user is anonymous, it will stay in this activity
                    Toast.makeText(getApplicationContext(), "Please sign up or login", Toast.LENGTH_LONG).show();
                }else{
                    // If the current user is NOT anonymous
                    // Get current user data from PARSE.com
                    ParseUser currentUser = ParseUser.getCurrentUser();
                    if (currentUser != null) {
                        //Send loged in user to MainActivity.class
                        // Send user to Welcome.class
                        ParseObject object = ParseObject.create(" User");
                        object.put("cenakutije", boxWorthTxt);
                        object.put("kolicinakutije", cigsInABoxTxt);
                        object.put("cigaretanadan", cigsInADayTxt);
                        object.put("duzinapusenja", howLongTxt);

                        object.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {
                                if(e == null) {
                                    setResult(RESULT_OK);
                                    finish();
                                }else{
                                    Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }else{
                        Toast.makeText(getApplicationContext(),"ono trece",Toast.LENGTH_LONG).show();
                    }
                }


            }

                ParseObject object = ParseObject.create("_User");
                object.put("cenakutije", boxWorthTxt);
                object.put("kolicinakutije", cigsInABoxTxt);
                object.put("cigaretanadan", cigsInADayTxt);
                object.put("duzinapusenja", howLongTxt);

                object.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if(e == null) {
                            setResult(RESULT_OK);
                            finish();
                        }else{
                            Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    }
                });


            }});}

        }

1 个答案:

答案 0 :(得分:0)

我相信您还需要致电logInInBackground()来实际创建会话。注册正在创建一个新用户,其中登录是获取会话参数并设置当前用户。

我的应用程序中的结构是这样的:我有一个新用户可以注册的注册活动,以及现有用户可以登录的登录活动。注册活动调用signUpInBackground()方法,登录活动调用logInInBackground()的位置。当注册活动完成signUpInBackground调用时,我会在此活动上调用finish(),并将其带到登录活动中,他们需要再次输入凭据并登录。