您好我从Android开始开始,我需要同时在两个表中进行用户注册。 我有User表,我有Patient和Caregiver表,在Register Activity中我有两个单选按钮来检查用户类型。如果"患者"选择然后我想插入用户和患者。 但是,我的无线电组(我认为)总是返回null。我检查了其他帖子并尝试了不同的解决方案,但它们都没有用。
所以我的 RegisterActivity 代码:
public class RegisterActivity extends AppCompatActivity {
DatabaseHelper helper = new DatabaseHelper(this);
private AlphaAnimation buttonClick = new AlphaAnimation(0.0F, 0.8F);
//RadioButton rb1, rb2, rb3;
RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
//rb1 = (RadioButton) findViewById(R.id.radioButton);
//rb2 = (RadioButton) findViewById(R.id.radioButton2);
}
//get selected radio button id (also returns null)
/* public int getIDRadio() {
int idRadio = radiogroup.getCheckedRadioButtonId();
if (idRadio == R.id.radioButton) {
return 1; //patient
} else if (idRadio == R.id.radioButton2) {
return 2; //caregiver
} else {
return -1; //erro não esta nenhum selecionado
}
}*/
//another try
public boolean radioGroup(){
int checkedID = radiogroup.getCheckedRadioButtonId(); //LINE 51
if(checkedID == R.id.radioButton) {
return true; //for patient
} else {
return false; //for caregiver
}
//return radiogroup.getCheckedRadioButtonId() == R.id.radioButton;
}
public void onRegisterClick(View v) {
if (v.getId() == R.id.REGISTARBUTTON) {
EditText name = (EditText) findViewById(R.id.TFNAME);
EditText email = (EditText) findViewById(R.id.TFEMAIL);
EditText uname = (EditText) findViewById(R.id.TFUNAME);
EditText password1 = (EditText) findViewById(R.id.TFPASSWORD1);
EditText password2 = (EditText) findViewById(R.id.TFPASSWORD2);
String namestr = name.getText().toString();
String emailstr = email.getText().toString();
String unamestr = uname.getText().toString();
String pass1str = password1.getText().toString();
String pass2str = password2.getText().toString();
if (namestr.isEmpty() || emailstr.isEmpty() || unamestr.isEmpty() || pass1str.isEmpty()) { //se os campos estiverem null
Toast fields = Toast.makeText(RegisterActivity.this, "Por favor, preencha os campos!", Toast.LENGTH_SHORT);
fields.show();
} else if (!pass1str.equals(pass2str)) { //se as passwords não forem iguais
Toast pass = Toast.makeText(RegisterActivity.this, "Password incorrecta, tente de novo!", Toast.LENGTH_SHORT);
pass.show();
/*} else if (getIDRadio() == -1) { //se nenhum radiobutton estiver selecionado
Toast userType = Toast.makeText(RegisterActivity.this, "Por favor, selecione um tipo de utilizador!", Toast.LENGTH_SHORT);
userType.show();*/
} else {
if (radioGroup()) {
//if patient radiobutton selected inserts into user and patient tables
UsersRegister userPatient = new UsersRegister();
userPatient.setName(namestr);
userPatient.setEmail(emailstr);
userPatient.setUname(unamestr);
userPatient.setPassword(pass1str); //pass1 ou pass2 e igual
Patient patient = new Patient();
patient.setName(namestr);
patient.setEmail(emailstr);
patient.setPassword(pass1str);
helper.insertUser(userPatient); //LINE 103
} else { //inserts into user and caregiver tables (...)
现在我的 RegisterActivity Layout :
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:layout_marginBottom="90dp"
android:orientation="horizontal"
android:id="@+id/radiogroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paciente"
android:id="@+id/radioButton"
android:checked="false"
android:layout_marginLeft="50dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cuidador"
android:id="@+id/radioButton2"
android:layout_marginLeft="50dp"/>
</RadioGroup>
Logcat
Process: com.example.dc.smoveen, PID: 25361
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3848)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3843)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.dc.smoveen.RegisterActivity.radioGroup(RegisterActivity.java:51)
at com.example.dc.smoveen.DatabaseHelper.insertUser(DatabaseHelper.java:108)
at com.example.dc.smoveen.RegisterActivity.onRegisterClick(RegisterActivity.java:103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3843)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
因为它总是返回null,给出NullPointerException错误,我无法进行注册。
任何人都可以帮助我发现我做错了什么吗?