[英]cant check for radio button in android studio Login/Reister app
每次我按下注冊按鈕時,它都會關閉應用程序,當我從 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())) {
我嘗試了太多的解決方案來初始化兩個單選按鈕並創建條件
if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && !radioButton1.isChecked() && !radioButton2.isChecked()) {
return;
}
和同樣的問題並嘗試過
if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && Gender != "Male" && Gender != "Female") {
return;
}
每次我按注冊時都不會關閉
錯誤
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>
這條線是錯誤的。
radioButton = (RadioButton) findViewById(selectedId);
您的 xml 單選按鈕屬性中的 id 應該相同。
在您的情況下,您應該像這樣初始化兩個單選按鈕
radioBtn2 = (RadioButton)findViewById(R.id.radioButton2);
radioBtn3 = (RadioButton)findViewById(R.id.radioButton3);
由於您想從兩個單選按鈕中獲取文本,您必須聲明兩個字符串。
String gender1 = radioBtn2.getText().toString();
String gender2 = radioBtn3.getText().toString();
最后
if (fName.isEmpty() && lName.isEmpty() && eUsername.isEmpty() && ePassword.isEmpty() && gender1 != "Male" && gender2 != "Female") {
return;
}
要比較 String,我們應該使用.equals()
。 對於不等於,它應該是
if (!gender1.equals("Male")) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.