2015-11-05 60 views
0

我在我的android工作室應用程序中做了一個註冊表操作(java),並且我有一個用於性別的廣播組。當我點擊單選按鈕來選擇性別時,我遇到了應用程序崩潰問題。林不知道如果我的代碼是正確的獲取信息的解析,或者存儲IM試圖解析存儲是怎樣的問題單選按鈕解析android studio

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 protected]"); 

// 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 
           } 

          } 
         }); 


        } 
       }); 



       }); 
      } 
+0

嘗試將您的代碼片段轉換爲簡短的相關示例。 – ColBeseder

回答

1

我覺得初始化單選按鈕,然後在呼喚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 protected]"); 

    // 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 
            } 

           } 
          }); 


         } 
        }); 



        }); 
       }