Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';无法从对话框中获取CheckedRadioButtonId_Android - Fatal编程技术网

Android Can';无法从对话框中获取CheckedRadioButtonId

Android Can';无法从对话框中获取CheckedRadioButtonId,android,Android,这是我在Stackoverflow上的第一篇文章,到目前为止,在这里找到很多信息非常好,谢谢 我正在尝试创建一个包含可选项的列表。当您按下该项时,它可能会提示一个带有3个单选按钮的对话框。我想从选中的单选按钮获取ID,但目前它总是返回相同的值。因为我默认了选项1(克莱因)。按另一个按钮并获取该值不会有帮助 我期待着你能为我提供的提示/技巧/帮助(我最近开始使用android编程,所以请纠正我的错误!) 我的xml(dialog.xml): 更新的代码: .setPositiveButton("

这是我在Stackoverflow上的第一篇文章,到目前为止,在这里找到很多信息非常好,谢谢

我正在尝试创建一个包含可选项的列表。当您按下该项时,它可能会提示一个带有3个单选按钮的对话框。我想从选中的单选按钮获取ID,但目前它总是返回相同的值。因为我默认了选项1(克莱因)。按另一个按钮并获取该值不会有帮助

我期待着你能为我提供的提示/技巧/帮助(我最近开始使用android编程,所以请纠正我的错误!)

我的xml(dialog.xml):


更新的代码:

.setPositiveButton("Bestellen", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int id) {

        //keep this variables global outside onCreate just like ExpandableListView expListView;
        RadioGroup radioGroup1; //this
        RadioButton radioButton; // and this too keep global

        radioGroup1 = (RadioGroup)myView.findViewById(R.id.RadioGroup);
        int something = radioGroup1.getCheckedRadioButtonId();
        radioButton = (RadioButton)myView.findViewById(something);
        String radioValue = radioButton.getText().toString();

        dialog.cancel();

        Toast.makeText(MainActivity.this, radioValue,
                Toast.LENGTH_SHORT).show();
    }
});
从XML中删除属性:

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup"
android:orientation="horizontal"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checkedButton="@+id/radioButton1" //removing this helped me so try to remove and apply the java code. 
>

我想出来了

我有:

LayoutInflater li=LayoutInflater.from(上下文); 最终视图myView=li.充气(R.layout.dialog,null)

此Build.setView应参考我的充气机。。。不是XML本身


So Builder.setView(myView);解决了这个问题

早上好,我已经尝试删除dialog.cancle,但这没有帮助,因为它仍然会得到导致应用程序崩溃的空值。。有什么方法可以让我和你分享我的屏幕,你可以浏览我的代码吗?问题是,它接受android studio上的代码。当我在手机上启动时:选择Frites:Patat Frites 1,60,然后选择3个单选按钮中的1个,单击:bestellen,由于空引用,它崩溃了@W4R10CKIt在我这边工作,非常清楚。试着初始化3个单选按钮,看看你能不能把你的代码放在pastebin上,这样我就可以测试并看看我的代码有什么不同?提前谢谢!
.setPositiveButton("Bestellen", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int id) {

        //keep this variables global outside onCreate just like ExpandableListView expListView;
        RadioGroup radioGroup1; //this
        RadioButton radioButton; // and this too keep global

        radioGroup1 = (RadioGroup)myView.findViewById(R.id.RadioGroup);
        int something = radioGroup1.getCheckedRadioButtonId();
        radioButton = (RadioButton)myView.findViewById(something);
        String radioValue = radioButton.getText().toString();

        dialog.cancel();

        Toast.makeText(MainActivity.this, radioValue,
                Toast.LENGTH_SHORT).show();
    }
});
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup"
android:orientation="horizontal"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checkedButton="@+id/radioButton1" //removing this helped me so try to remove and apply the java code. 
>
                final AlertDialog.Builder Builder = new AlertDialog.Builder(context);


                // set layout
                Builder.setView(dialog);