Android - 带有单选按钮的AlertDialog,获取AndroidRuntimeException

时间:2012-11-16 18:19:21

标签: android exception radio-button alertdialog

我正在开发一个使用SQLite数据库来存储信息的Android应用程序。我正在尝试获取其中一些信息,并将它们添加到带有radiobuttons的AlertDialog中,但是当我调试时,我得到此RuntimeException:在添加内容之前必须调用requestFeature() 我认为错误在于:

  

shop_type.setContentView(R.layout.shop_list_selection);

但我不知道如何解决它。

点击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();
          }
      }
    });

这里是XML布局:

<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>

2 个答案:

答案 0 :(得分:1)

当您收到错误requestFeature() must be called before adding content时,表示您正在尝试以错误的顺序构建窗口 为了避免这个错误,我建议利用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();

答案 1 :(得分:0)

在requestFeature()之后调用setContentView()。

call shop_type.show();在AlertDialog shop_type = new AlertDialog.Builder之后(MenuActivity.this).create();