单选按钮解析 android 工作室

Radio Button parse android studio

我正在我的 android 工作室应用程序中进行注册操作 (java),并且我有一个性别广播组。当我单击 select 性别的单选按钮时,我遇到了应用程序崩溃的问题。我不确定我的代码是否适合将信息存储在解析中,或者我尝试存储在解析中的方式是否有问题

    enter code here

    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CompoundButton;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;

    import com.parse.ParseException;
    import com.parse.ParseUser;
    import com.parse.SignUpCallback;

    public class RegisterActivity extends AppCompatActivity {


    protected EditText mUserFirstname;
    protected EditText mUserLastname;
    protected EditText mUsername;
    protected EditText mUserPassword;
    protected EditText mUserEmail;
    protected EditText mUserPhonenumber;
    protected EditText mUserPostalcode;
    protected RadioGroup mGender;
    protected RadioButton mMale;
    protected RadioButton mFemale;
    protected EditText mDob;
    protected Button mRegisterbutton;
    private static final String COLUMN_GENDER = ""; //put here name of your column from table on Parse.com
    private static final String MALE = ""; //put here value for male. Check the type of gender column on Parse.com (String, Number (Integer), Boolean, etc.)
    private static final String FEMALE = ""; //put here value for female. Check the type of gender column on Parse.com (String, Number (Integer), Boolean, etc.)


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

       mMale.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if (isChecked) {ParseUser user = new ParseUser();
                   user.put(COLUMN_GENDER, MALE);
                   user.saveInBackground(); // if you want to save immediately

               }
           }
       });

        mFemale.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {ParseUser user = new ParseUser();
                    user.put(COLUMN_GENDER, FEMALE);
                    user.saveInBackground(); // if you want to save immediately
                }
            }
        });



                //initialize
                mUserFirstname = (EditText) findViewById(R.id.firstNameRegisterEditText);
                mUserLastname = (EditText) findViewById(R.id.lastNameRegisterEditText);
                mUsername = (EditText) findViewById(R.id.userNameRegisterEditText);
                mUserPassword = (EditText) findViewById(R.id.passwordRegisterEditText);
                mUserEmail = (EditText) findViewById(R.id.emailRegisterEditText);
                mUserPhonenumber = (EditText) findViewById(R.id.phoneNumberRegisterEditText);
                mUserPostalcode = (EditText) findViewById(R.id.postalCodeRegisterEditText);
                mGender = (RadioGroup) findViewById(R.id.radioGroup);
                mMale = (RadioButton) findViewById(R.id.maleGenderRegisterEditText);
                mFemale = (RadioButton) findViewById(R.id.femaleGenderRegisterEditText);
                mDob = (EditText) findViewById(R.id.dateofBirthEditText);
                mRegisterbutton = (Button) findViewById(R.id.registerButton);



                //Listen to Register button click
                mRegisterbutton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //toast

                        //get the username, password and email and convert to string

                        String username = mUsername.getText().toString().trim();
                        String password = mUserPassword.getText().toString().trim();
                        String email = mUserEmail.getText().toString().trim();
                        String firstname = mUserFirstname.getText().toString().trim();
                        String lastname = mUserLastname.getText().toString().trim();
                        String phonenumber = mUserPhonenumber.getText().toString().trim();
                        String postalcode = mUserPostalcode.getText().toString().trim();
                        String gender = mMale.isChecked() ? MALE : FEMALE;
                        //store user
                        ParseUser user = new ParseUser();
                        user.setUsername("my name");
                        user.setPassword("my pass");
                        user.setEmail("email@example.com");

// other fields can be set just like with ParseObject
                        user.put("first name", "my name");
                        user.put("last name", "my name");
                        user.put("postal", "65055");
                        user.put("gender", "male or female");
                        user.put("dob", "mm/dd/yyyy");
                        user.put("phone", "650-253-0000");
                        user.put(COLUMN_GENDER, gender);

                        user.signUpInBackground(new SignUpCallback() {
                            @Override
                            public void done(ParseException e) {
                                if (e==null) {
                                    //user signed up succesfully
                                    Toast.makeText(RegisterActivity.this,"Welcome To The Party...", Toast.LENGTH_LONG).show();

                                    //take user to homepage
                                }else {
                                // there was and error sigingup user. advise user
                                }

                            }
                        });


                    }
                });



                });
            }

我认为您是在初始化单选按钮之前调用 setOnCheckedChangeListener()。因此,请尝试将初始化程序移到顶部,然后是单选按钮。

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

           //initialize first
                    mUserFirstname = (EditText) findViewById(R.id.firstNameRegisterEditText);
                    mUserLastname = (EditText) findViewById(R.id.lastNameRegisterEditText);
                    mUsername = (EditText) findViewById(R.id.userNameRegisterEditText);
                    mUserPassword = (EditText) findViewById(R.id.passwordRegisterEditText);
                    mUserEmail = (EditText) findViewById(R.id.emailRegisterEditText);
                    mUserPhonenumber = (EditText) findViewById(R.id.phoneNumberRegisterEditText);
                    mUserPostalcode = (EditText) findViewById(R.id.postalCodeRegisterEditText);
                    mGender = (RadioGroup) findViewById(R.id.radioGroup);
                    mMale = (RadioButton) findViewById(R.id.maleGenderRegisterEditText);
                    mFemale = (RadioButton) findViewById(R.id.femaleGenderRegisterEditText);
                    mDob = (EditText) findViewById(R.id.dateofBirthEditText);
                    mRegisterbutton = (Button) findViewById(R.id.registerButton);

      //listen to checked changes next
           mMale.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   if (isChecked) {ParseUser user = new ParseUser();
                       user.put(COLUMN_GENDER, MALE);
                       user.saveInBackground(); // if you want to save immediately

                   }
               }
           });

            mFemale.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {ParseUser user = new ParseUser();
                        user.put(COLUMN_GENDER, FEMALE);
                        user.saveInBackground(); // if you want to save immediately
                    }
                }
            });







                    //Listen to Register button click
                    mRegisterbutton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //toast

                            //get the username, password and email and convert to string

                            String username = mUsername.getText().toString().trim();
                            String password = mUserPassword.getText().toString().trim();
                            String email = mUserEmail.getText().toString().trim();
                            String firstname = mUserFirstname.getText().toString().trim();
                            String lastname = mUserLastname.getText().toString().trim();
                            String phonenumber = mUserPhonenumber.getText().toString().trim();
                            String postalcode = mUserPostalcode.getText().toString().trim();
                            String gender = mMale.isChecked() ? MALE : FEMALE;
                            //store user
                            ParseUser user = new ParseUser();
                            user.setUsername("my name");
                            user.setPassword("my pass");
                            user.setEmail("email@example.com");

    // other fields can be set just like with ParseObject
                            user.put("first name", "my name");
                            user.put("last name", "my name");
                            user.put("postal", "65055");
                            user.put("gender", "male or female");
                            user.put("dob", "mm/dd/yyyy");
                            user.put("phone", "650-253-0000");
                            user.put(COLUMN_GENDER, gender);

                            user.signUpInBackground(new SignUpCallback() {
                                @Override
                                public void done(ParseException e) {
                                    if (e==null) {
                                        //user signed up succesfully
                                        Toast.makeText(RegisterActivity.this,"Welcome To The Party...", Toast.LENGTH_LONG).show();

                                        //take user to homepage
                                    }else {
                                    // there was and error sigingup user. advise user
                                    }

                                }
                            });


                        }
                    });



                    });
                }