Reputation: 165
I'm developing an Android App which use a SQLite database to store informations. I'm trying to get some of these informations and add them into an AlertDialog with radiobuttons, but when I debug I get this RuntimeException: requestFeature() must be called before adding content I think the error is in:
shop_type.setContentView(R.layout.shop_list_selection);
but I don't know how to fix it.
Here the code when I click on a ImageButton:
ImageButton shopping = (ImageButton)findViewById(R.id.gotoshop); shopping.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Get infos from DB Cursor lists = dbAdapter.select("ls_tipo_lista", new String[] {"id", "nome"}, null, null, null, null, null); // Fetch data if exists if(lists.getCount() > 0) { // Create AlertDialog AlertDialog shop_type = new AlertDialog.Builder(MenuActivity.this).create(); // Set title and layout shop_type.setTitle("Tipo di spesa"); shop_type.setContentView(R.layout.shop_list_selection); // Get RadioGroup from shop_list_selection.xml RadioGroup opts = (RadioGroup)shop_type.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); } // Show dialog shop_type.show(); } } });
Here the XML layout:
<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>
Upvotes: 0
Views: 1048
Reputation: 364
Call setContentView() after requestFeature().
call shop_type.show(); just after AlertDialog shop_type = new AlertDialog.Builder(MenuActivity.this).create();
Upvotes: 0
Reputation: 86948
When you get the error requestFeature() must be called before adding content
it means you are trying to build your window in the wrong order.
To avoid this error, I suggest taking advantage of the AlertDialog.Builder class:
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();
Upvotes: 1