简体   繁体   English

无法在 android studio 登录/注册应用程序中检查单选按钮

[英]cant check for radio button in android studio Login/Reister app

every time i press register button it close the application and when i remove the radio button from the if condition and make one of the radio buttons checked from xml at activity start it works fine her is the code每次我按下注册按钮时,它都会关闭应用程序,当我从 if 条件中删除单选按钮并在活动开始时从 xml 检查单选按钮之一时,它工作正常,她是代码


 // get selected radio button from radioGroup
                  int selectedId = radioGroup.getCheckedRadioButtonId();

              // find the radiobutton by returned id
              radioButton = (RadioButton) findViewById(selectedId);

              String Gender = radioButton.getText().toString();

              if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && radioGroup.getCheckedRadioButtonId() < 0) {
                  return;
              }

              else if (fName.isEmpty() || lName.isEmpty() || eUsername.isEmpty() || ePassword.isEmpty() || radioGroup.getCheckedRadioButtonId() < 0) {
                  Snackbar.make(view, "Please enter your full data", Snackbar.LENGTH_LONG)
                          .setAction("Action", null).show();
              }


              else {
                  //********************************************************************************
                  if(Objects.equals(password.getText().toString(),RPTpassword.getText().toString())) {

i tried too many solution as initializing the two radio buttons and make condition我尝试了太多的解决方案来初始化两个单选按钮并创建条件


if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && !radioButton1.isChecked() && !radioButton2.isChecked()) {
                      return;
                  }

and the same problem and tried和同样的问题并尝试过


if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && Gender != "Male" && Gender != "Female") {
                          return;
                      }

and nothing it close every time i press register每次我按注册时都不会关闭

the error错误


 AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.android.loginregister, PID: 3713
                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
                          at com.android.loginregister.RegisterActivity$2.onClick(RegisterActivity.java:131)
                          at android.view.View.performClick(View.java:5610)
                          at android.view.View$PerformClick.run(View.java:22265)
                          at android.os.Handler.handleCallback(Handler.java:751)
                          at android.os.Handler.dispatchMessage(Handler.java:95)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

<RadioGroup
            android:id="@+id/rdioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="25dp"
            android:layout_below="@id/RePassword">

            <RadioButton
                android:text="Male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton3"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:textAllCaps="false"
                android:textColor="#FFFFFF"
                android:buttonTint="#FFFFFF"
                android:textSize="14sp"
                android:textColorLink="#e8d829"
                android:focusableInTouchMode="false"
                android:checked="false" />

            <RadioButton
                android:text="Female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton2"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:textAllCaps="false"
                android:textColor="#FFFFFF"
                android:buttonTint="#FFFFFF"
                android:textSize="14sp"
                android:textColorLink="#2362e0" />

        </RadioGroup>

This line is wrong.这条线是错误的。

 radioButton = (RadioButton) findViewById(selectedId);

The id should be same in your xml radioButton attributes.您的 xml 单选按钮属性中的 id 应该相同

In your case, you should initialize two radio buttons like this在您的情况下,您应该像这样初始化两个单选按钮

radioBtn2 = (RadioButton)findViewById(R.id.radioButton2);
radioBtn3 = (RadioButton)findViewById(R.id.radioButton3);

And since you want to get the text from two radioButtons, you have to declare two Strings.由于您想从两个单选按钮中获取文本,您必须声明两个字符串。

String gender1 = radioBtn2.getText().toString();
String gender2 = radioBtn3.getText().toString();

And finallly最后

  if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && gender1 != "Male" && gender2 != "Female") {
     return;
   }

To compare String, we should use .equals() .要比较 String,我们应该使用.equals() For not equals, it should be对于不等于,它应该是

if (!gender1.equals("Male")) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM