使用微调器获取非法状态异常

时间:2012-02-28 13:46:38

标签: android android-arrayadapter android-spinner

您好我正在尝试使用arrayadapter填充带有字符串的微调器。但是我无法这样做,因为它会抛出illegalstateexception

这是代码

   final Dialog dialog= new Dialog(SamplepaintActivity.this);
    dialog.setContentView(R.layout.customhourdialog);

    Button OK,Cancel;

    OK = (Button)findViewById(R.id.OkButton);
    Cancel = (Button)findViewById(R.id.CancelButton);

    ArrayAdapter <String> spinAdapter =
              new ArrayAdapter <String> (this, android.R.layout.simple_spinner_item );
            spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinAdapter.add("Sample1");
            spinAdapter.add("Sample2");
            Spinner spinner = (Spinner)findViewById(R.id.spinner1);
    spinner.setAdapter(spinAdapter);

    Cancel.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {



                dialog.dismiss();

         }});
    OK.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {



                dialog.dismiss();

         }});
    dialog.show();

当我为微调器

设置适配器时,该行中会抛出异常

这是我的customdialog

的xml文件
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
      android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="wrap_content" android:text="Begin" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_toRightOf="@+id/textView1" android:layout_marginLeft="35dp"></Button>
     <Button android:layout_height="wrap_content" android:id="@+id/button2" android:layout_width="wrap_content" android:text="End" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/button1" android:layout_marginLeft="56dp"></Button>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Subject" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView2" android:layout_below="@+id/button1" android:layout_alignParentLeft="true" android:layout_marginTop="39dp"></TextView>
<Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner1" android:layout_alignTop="@+id/textView2" android:layout_alignLeft="@+id/button1" android:layout_alignRight="@+id/button2">

</Spinner>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Time" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="20dp"></TextView>
<Button android:layout_height="wrap_content" android:id="@+id/OkButton" android:layout_width="150dp" android:text="OK" android:layout_below="@+id/spinner1" android:layout_alignParentLeft="true" android:layout_marginTop="53dp"></Button>
<Button android:layout_height="wrap_content" android:text="Cancel" android:layout_width="150dp" android:id="@+id/CancelButton" android:layout_alignBaseline="@+id/OkButton" android:layout_alignBottom="@+id/OkButton" android:layout_alignParentRight="true"></Button>

2 个答案:

答案 0 :(得分:1)

我认为问题出在这一行:

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

适配器更改为 spinAdapter ,您应该好好去。见下文:

spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

<强>更新 您需要修复Dialog构建过程。见下文:

    AlertDialog.Builder bldr = new AlertDialog.Builder(SamplepaintActivity.this);
    LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflator.inflate(R.layout.customhourdialog, null);

    bldr.setView(view);

    Button OK,Cancel;

    OK = (Button)view.findViewById(R.id.OkButton);
    Cancel = (Button)view.findViewById(R.id.CancelButton);
    Spinner spinner = (Spinner)view.findViewById(R.id.spinner1);
    ArrayAdapter <String> spinAdapter = new ArrayAdapter <String> (this, android.R.layout.simple_spinner_item );
    spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinAdapter.add("Sample1");
    spinAdapter.add("Sample2");
    spinner.setAdapter(spinAdapter);

    bldr.show();

答案 1 :(得分:0)

首先定义微调器,然后定义适配器。之后会看到错误发生的位置。我想错误不会发生。