2015-04-18 37 views
0

我有两个活动,第一个是注册活动,第二个是我想向第一个活动添加一些数据到同一个数据库。 错误,当我点击在RegisterInformationActivity BTN这是我得到的是: “无法保存parseuser,直到它已签署了”将数据添加到Parse.com Android

第一项活动:

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


      }});} 

     } 

回答

0

我相信你还需要拨打logInInBackground()来实际创建会话。注册正在创建一个新用户,登录时需要获取会话参数并设置当前用户。

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

+0

这样做对您有帮助吗? – Urban

+0

对不起,迟到的答案。我很忙。 我无法调用logInInBackground()。 –

+0

为什么不能调用loginInBackground? – Urban