【问题标题】:Android - AlertDialog with RadioButtons, getting AndroidRuntimeExceptionAndroid - 带有 RadioButtons 的 AlertDialog,获取 AndroidRuntimeException
【发布时间】:2012-11-17 03:16:33
【问题描述】:

我正在开发一个使用 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>

【问题讨论】:

    标签: android exception radio-button android-alertdialog


    【解决方案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();
    

    【讨论】:

    • 哦,太好了。它完美地工作。谢谢! :) 澄清:setView() 不是简单 AlertDialog 的方法,仅适用于 AlertDialog.Builder :)
    • 很高兴能帮上忙。技术方面 setView(int) 不是 AlertDialog 中的方法,但 setView(View) 是……抱歉,我更新了答案。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      相关资源
      最近更新 更多