RadioGroup问题

时间:2017-01-06 22:06:11

标签: java android radio-button radio-group

我试图从radiogroup获取radiobutton文本,但问题是:它没有得到值。我在其他链接上搜索但对我没有帮助。

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



    }
}

我收到了这个错误:

  

致命的例外:主要                                                                                进程:associate.aclass.associate1,PID:28847                                                                                java.lang.NullPointerException:尝试调用虚方法   'java.lang.CharSequence android.widget.RadioButton.getText()'上一个   null对象引用                                                                                    在   associate.aclass.associate1.registration_form_handler $ 2.onClick(registration_form_handler.java:81)                                                                                    在android.view.View.performClick(View.java:4856)                                                                                    在android.view.View $ PerformClick.run(View.java:19956)                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:211)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5389)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在java.lang.reflect.Method.invoke(Method.java:372)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1020)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

1 个答案:

答案 0 :(得分:1)

btn_gender为null您需要在Gender.setOnCheckedChangeListener之外执行此操作以初始化btn_gender

btn_gender = (RadioButton)findViewById(R.id.radioButtonID); // use your id