android - 在自定义警报对话框中获取单选按钮的值

标签 android android-alertdialog android-radiobutton android-radiogroup

我创建了一个带有单选组、单选按钮和编辑文本的自定义警报对话框。

目标是尝试从选定的单选按钮和编辑文本中获取文本值。此时,我只能获取在edittext中输入的值。

我看到了下面的链接,尝试调整代码适配,但是好像代码还是不行。

Android getting value from selected radiobutton

http://www.mkyong.com/android/android-radio-buttons-example/

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<RadioGroup
    android:id="@+id/radioPersonGroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RadioButton
        android:id="@+id/gButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:text="G" />
    <RadioButton
        android:id="@+id/kButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:text="K" />

</RadioGroup>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    android:layout_weight="1"
    android:ems="10"
    android:hint="$10.00"
    android:inputType="numberDecimal" />
</LinearLayout>

java文件

private RadioButton radioSelectedButton;
...

FloatingActionButton fab = findViewById(R.id.fab);
final RadioGroup group = findViewById(R.id.radioPersonGroup);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        final View dialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
        builder.setTitle("Who Paid")
            .setView(dialogView)
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    int selectedId = group.getCheckedRadioButtonId();
                    radioSelectedButton = (RadioButton) findViewById(selectedId);
                    Toast.makeText(MainActivity.this,
            radioSelectedButton.getText(), Toast.LENGTH_SHORT).show();

                            EditText input = dialogView.findViewById(R.id.editText2);
                            Toast.makeText(MainActivity.this,input.getText().toString(), Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                        }
                    });

            AlertDialog alert = builder.create();
            alert.show();
        }
    });

当我单击任一按钮然后单击确定以提交对话框时,会发生以下异常。

java.lang.NullPointerException:尝试在空对象引用上调用虚方法“int android.widget.RadioGroup.getCheckedRadioButtonId()”

enter image description here

最佳答案

你得到一个 NullPointerException在这一行

int selectedId = group.getCheckedRadioButtonId();

因为您试图“找到”RadioGroup打错了View当你写的时候

final RadioGroup group = findViewById(R.id.radioPersonGroup);

RadioGroupDialog 的一部分, 所以你需要在 View 中寻找它dialogView 树:

// inside onClick()
final View dialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
final RadioGroup group = dialogView.findViewById(R.id.radioPersonGroup);

同样,您需要在 ViewGroup 中“找到”selectedRadioButton其中包含它。例如

radioSelectedButton = (RadioButton) dialogView.findViewById(selectedId);

radioSelectedButton = (RadioButton) group.findViewById(selectedId);

关于android - 在自定义警报对话框中获取单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54353232/

相关文章:

java - Android 将数据存储在服务器中

android - ScrollView 中的 ViewPagerIndicator

Android - key 调度超时

Android:从服务访问单选按钮值

android - 如何只设置一个 RadioButton 可以在 RadioGroup 中当时选择

java - Android Firebase 2.4 IllegalStateException 使用新的 ref.updateChildren()

android - 通过对话框更新 fragment ListView 中显示的数据库条目

Android Kotlin findViewById 不得为 null

java - AlertDialog : After showing the dialog, 背景 Canvas 渲染继续,但对话框永远不会关闭

java - 另一个 Activity 中的单选按钮仍然为空?