2012-11-16 34 views
0

我正在開發一個Android應用程序,它使用SQLite數據庫存儲信息。我試圖得到一些這些信息,並將它們添加到AlertDialog與單選按鈕,但是當我調試,我得到這樣的RuntimeException:requestFeature()必須在添加內容 之前調用我認爲錯誤是:Android - AlertDialog與RadioButtons,得到AndroidRuntimeException

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> 

回答

1

當你的錯誤requestFeature() must be called before adding content這意味着你正試圖以錯誤的順序建立你的窗口。
爲了避免這種錯誤,我建議採取的AlertDialog.Builder類的優勢:requestFeature後

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(); 
+0

哦,那太好了。它完美的作品。謝謝! :)澄清:setView()不是簡單的AlertDialog的方法,只適用於AlertDialog.Builder :) – Andres7X

+0

我很高興我可以提供幫助。要成爲技術'setView(int)'不是AlertDialog中的方法,但是'setView(View)'是......對於我更新我的答案的困惑感到抱歉。 – Sam

0

調用的setContentView()()。

call shop_type.show();緊隨AlertDialog shop_type = new AlertDialog.Builder(MenuActivity.this).create();