Android-带有单选按钮的AlertDialog,获取AndroidRuntimeException
我正在开发一个Android应用程序,它使用SQLite数据库来存储信息。我正试图获取其中一些信息,并将它们添加到带有单选按钮的AlertDialog中,但当我调试时,会出现以下运行时异常:requestFeature()必须在添加内容之前调用 我认为错误在于: 商店类型。设置内容视图(R.布局。商店列表选择) 但我不知道如何修复它 这里是我单击ImageButton时的代码: 以下是XML布局:Android-带有单选按钮的AlertDialog,获取AndroidRuntimeException,android,exception,radio-button,android-alertdialog,Android,Exception,Radio Button,Android Alertdialog,我正在开发一个Android应用程序,它使用SQLite数据库来存储信息。我正试图获取其中一些信息,并将它们添加到带有单选按钮的AlertDialog中,但当我调试时,会出现以下运行时异常:requestFeature()必须在添加内容之前调用 我认为错误在于: 商店类型。设置内容视图(R.布局。商店列表选择) 但我不知道如何修复它 这里是我单击ImageButton时的代码: 以下是XML布局: 当您遇到错误时,在添加内容之前必须调用requestFeature(),这意味着您试图以错误的
当您遇到错误时,在添加内容之前必须调用
requestFeature(),这意味着您试图以错误的顺序构建窗口。
为了避免此错误,我建议利用AlertDialog.Builder类:
AlertDialog.Builder builder = new AlertDialog.Builder(MenuActivity.this);
// Create the View to populate the RadioButtons
View view = LayoutInflater.from(MenuActivity.this).inflate(R.layout.shop_list_selection, null, false);
// Get RadioGroup from shop_list_selection.xml
RadioGroup opts = (RadioGroup)view.findViewById(R.id.rbshop);
// Fetch data
while(lists.moveToNext())
{
// Create radio button instance
RadioButton rdbtn = new RadioButton(MenuActivity.this);
// Add data
rdbtn.setId(lists.getInt(0));
rdbtn.setText(lists.getString(1));
// Add to radiogroup
opts.addView(rdbtn);
}
// Set title and layout
builder.setTitle("Tipo di spesa");
builder.setView(view);
// Show dialog
AlertDialog shop_type = builder.create();
shop_type.show();
// This works too for a one-off dialog: builder.show();
在requestFeature()之后调用setContentView()
调用shop_type.show();紧接着AlertDialog shop_type=new AlertDialog.Builder(MenuActivity.this.create() 哦,太好了。它工作得很好。谢谢!:)澄清:setView()不是简单AlertDialog的方法,只适用于AlertDialog.Builder:)我很高兴能帮上忙。技术性的setView(int)
不是AlertDialog中的方法,但是setView(View)
是。。。很抱歉,我更新了我的答案。
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rbshop" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" > </RadioGroup>
AlertDialog.Builder builder = new AlertDialog.Builder(MenuActivity.this);
// Create the View to populate the RadioButtons
View view = LayoutInflater.from(MenuActivity.this).inflate(R.layout.shop_list_selection, null, false);
// Get RadioGroup from shop_list_selection.xml
RadioGroup opts = (RadioGroup)view.findViewById(R.id.rbshop);
// Fetch data
while(lists.moveToNext())
{
// Create radio button instance
RadioButton rdbtn = new RadioButton(MenuActivity.this);
// Add data
rdbtn.setId(lists.getInt(0));
rdbtn.setText(lists.getString(1));
// Add to radiogroup
opts.addView(rdbtn);
}
// Set title and layout
builder.setTitle("Tipo di spesa");
builder.setView(view);
// Show dialog
AlertDialog shop_type = builder.create();
shop_type.show();
// This works too for a one-off dialog: builder.show();