旋转器不会加载

时间:2014-06-26 13:40:19

标签: android android-layout

我正在尝试使用包含选项列表的微调器在android中创建弹出窗口(模态框)。此微调器位于片段活动中。我在我的onCreateDialog方法中添加了以下代码:

 @Override
 protected Dialog onCreateDialog(int id) {

  AlertDialog dialogDetails = null;

  switch (id) 
  {
  case DIALOG_LOGIN:
   LayoutInflater inflater = LayoutInflater.from(this);
   View dialogview = inflater.inflate(R.layout.dialog_layout, null);
   ((ItemListFragment) getSupportFragmentManager().findFragmentById(R.id.item_list))
   .setActivateOnItemClick(true);
   Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
   // Create an ArrayAdapter using the string array and a default spinner layout
  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
   R.array.planets_array, android.R.layout.simple_spinner_item);
  // Specify the layout to use when the list of choices appears
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  // Apply the adapter to the spinner
  spinner.setAdapter(adapter);

   break;
  }

  return dialogDetails;
 }

我的XML布局文件如下:

<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
android:orientation='vertical'
android:padding='10sp' >

<EditText
    android:id='@+id/txt_name'
    android:layout_width='fill_parent'
    android:layout_height='wrap_content'
    android:hint='"Username'
    android:singleLine='true' >

    <requestFocus />
</EditText>

<EditText
    android:id='@+id/password'
    android:layout_width='match_parent'
    android:layout_height='wrap_content'
    android:ems='10'
    android:inputType='textPassword' >
</EditText>

<RelativeLayout
    android:layout_width='match_parent'
    android:layout_height='wrap_content' >

    <Button
        android:id='@+id/btn_login'
        android:layout_width='120dp'
        android:layout_height='wrap_content'
        android:text='"Submit' />

    <Button
        android:id='@+id/btn_cancel'
        android:layout_width='120dp'
        android:layout_height='wrap_content'
        android:layout_alignParentTop='true'
        android:layout_marginLeft='10dp'
        android:layout_toRightOf='@+id/btn_login'
        android:text='"Cancel' />
</RelativeLayout>

<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

当我在Android平板电脑上打开应用程序时,它只是关闭了应用程序。当我创建自定义对话框并在onCreateDialog方法中添加代码时,它很好。然后我添加了微调器代码,它失败了。有人能告诉我我做错了什么以及如何让旋转器显示出来。

谢谢!

2 个答案:

答案 0 :(得分:0)

问题是你没有得到微调参考。请尝试以下代码: -

case DIALOG_LOGIN:
   LayoutInflater inflater = LayoutInflater.from(this);
   View dialogview = inflater.inflate(R.layout.dialog_layout, null);
   ((ItemListFragment) getSupportFragmentManager().findFragmentById(R.id.item_list))
   .setActivateOnItemClick(true);
   Spinner spinner = (Spinner) dialogview.findViewById(R.id.planets_spinner);
   // Create an ArrayAdapter using the string array and a default spinner layout
  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
   R.array.planets_array, android.R.layout.simple_spinner_item);
  // Specify the layout to use when the list of choices appears
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  // Apply the adapter to the spinner
  spinner.setAdapter(adapter);

   break;
  }

   Spinner spinner = (Spinner) dialogview.findViewById(R.id.planets_spinner);

您必须从参考视图中获取价值。

答案 1 :(得分:0)

这是我尝试过的,它似乎完全符合我的需要。我正在将微调器添加到片段中:

protected void onCreateDialog() 
{

    final Dialog settingdialog = new Dialog(ItemListActivity.this);  

    settingdialog.setContentView(R.layout.dialog_layout);  
    settingdialog.setTitle("Settings Menu");  
    settingdialog.show();  
    spinner = (Spinner)settingdialog.findViewById(R.id.planets_spinner);  

    //Populate the dropdown list
    String[] state = {"Cupcake", "Donut", "Eclair", "Froyo",
               "Gingerbread", "HoneyComb", "IceCream Sandwich", "Jellybean",
               "KitKat"};
    ArrayAdapter<String>adapter = new ArrayAdapter<String>(ItemListActivity.this,  
            android.R.layout.simple_spinner_item,state);  

    spinner.setAdapter(adapter);  

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
    {
        /**
         * Called when a new item is selected (in the Spinner)
         */
         public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) 
         {

               value = spinner.getItemAtPosition(pos).toString();
               String s = value;
               if (mTwoPane) 
               {

                   settingdialog.cancel();
                   Bundle arguments = new Bundle();
                   arguments.putInt("num", 1);
                   arguments.putString("value", s);
                   ItemListFragment fragment = new ItemListFragment();
                   fragment.setArguments(arguments);
                                         getSupportFragmentManager().beginTransaction().replace(R.id.item_list, fragment).commit();

               } 
               else 
               {

                   Intent detailIntent = new Intent(ItemListActivity.this, ItemListFragment.class);
                   detailIntent.putExtra("num", 1);
                   detailIntent.putExtra("value", s);
                   startActivity(detailIntent);
               }

            }

            public void onNothingSelected(AdapterView<?> parent) 
            {
            }

    }); 
 }