0

我正在开发一个使用 SQLite 数据库存储信息的 Android 应用程序。我正在尝试获取其中一些信息并将它们添加到带有单选按钮的 AlertDialog 中,但是当我调试时,我得到了这个 RuntimeException: requestFeature() must be called before added content 我认为错误在于:

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

2 回答 2

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();
于 2012-11-16T18:34:57.223 回答
0

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

调用 shop_type.show(); 就在 AlertDialog shop_type = new AlertDialog.Builder(MenuActivity.this).create();

于 2012-11-16T19:16:33.437 回答