RadioGroup 问题
RadioGroup issue
我正在尝试从单选组获取单选按钮文本,但问题是:它没有获得值。我搜索了其他链接,但对我没有帮助。
private EditText FirstName;
private EditText LastName;
private RadioGroup Gender;
private EditText MobileNo;
private RadioButton btn_gender;
private Button BtnNext;
private ProgressBar progressBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
Firebase.setAndroidContext(this);
FirstName=(EditText)findViewById(R.id.firstname);
LastName=(EditText)findViewById(R.id.lastname);
Gender=(RadioGroup) findViewById(R.id.radio_group);
MobileNo=(EditText)findViewById(R.id.btn_mobile);
BtnNext=(Button)findViewById(R.id.btn_regform);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
int selectedId=Gender.getCheckedRadioButtonId();
btn_gender=(RadioButton)findViewById(selectedId);
BtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
Firebase ref=new Firebase(config_firebaseurl.FIREBASE_URL);
String fname=FirstName.getText().toString().trim();
String lname=LastName.getText().toString().trim();
String gender=btn_gender.getText().toString().trim();
String phoneno=BtnNext.getText().toString().trim();
registration_form reg=new registration_form();
reg.setFirstName(fname);
reg.setLastName(lname);
reg.setGender(gender);
reg.setmobileNo(phoneno);
ref.child("registration_form").setValue(reg);
Toast.makeText(getApplicationContext(),"Registration Error", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
我收到了这个错误:
FATAL EXCEPTION: main
Process: associate.aclass.associate1, PID: 28847
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.CharSequence android.widget.RadioButton.getText()' on a
null object reference
at
associate.aclass.associate1.registration_form_handler.onClick(registration_form_handler.java:81)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
btn_gender 为 null 您需要在 Gender.setOnCheckedChangeListener 之外执行此操作以初始化 btn_gender
btn_gender = (RadioButton)findViewById(R.id.radioButtonID); // use your id
我正在尝试从单选组获取单选按钮文本,但问题是:它没有获得值。我搜索了其他链接,但对我没有帮助。
private EditText FirstName;
private EditText LastName;
private RadioGroup Gender;
private EditText MobileNo;
private RadioButton btn_gender;
private Button BtnNext;
private ProgressBar progressBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
Firebase.setAndroidContext(this);
FirstName=(EditText)findViewById(R.id.firstname);
LastName=(EditText)findViewById(R.id.lastname);
Gender=(RadioGroup) findViewById(R.id.radio_group);
MobileNo=(EditText)findViewById(R.id.btn_mobile);
BtnNext=(Button)findViewById(R.id.btn_regform);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
int selectedId=Gender.getCheckedRadioButtonId();
btn_gender=(RadioButton)findViewById(selectedId);
BtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
Firebase ref=new Firebase(config_firebaseurl.FIREBASE_URL);
String fname=FirstName.getText().toString().trim();
String lname=LastName.getText().toString().trim();
String gender=btn_gender.getText().toString().trim();
String phoneno=BtnNext.getText().toString().trim();
registration_form reg=new registration_form();
reg.setFirstName(fname);
reg.setLastName(lname);
reg.setGender(gender);
reg.setmobileNo(phoneno);
ref.child("registration_form").setValue(reg);
Toast.makeText(getApplicationContext(),"Registration Error", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
我收到了这个错误:
FATAL EXCEPTION: main Process: associate.aclass.associate1, PID: 28847 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference at associate.aclass.associate1.registration_form_handler.onClick(registration_form_handler.java:81) at android.view.View.performClick(View.java:4856) at android.view.View$PerformClick.run(View.java:19956) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
btn_gender 为 null 您需要在 Gender.setOnCheckedChangeListener 之外执行此操作以初始化 btn_gender
btn_gender = (RadioButton)findViewById(R.id.radioButtonID); // use your id