Getting illegal state exception with spinner
-
13-11-2019 - |
Question
Hi i am trying to a populate a spinner with strings using arrayadapter.but i am not able to do so as it throws illegalstateexception
Here is the code
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();
THe exception is thrown in the line when i set the adapter for the spinner
This is the xml file of my customdialog
<?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>
Solution
i think the problem is in this line:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
change adapter to spinAdapter and you should be good to go. see below:
spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
updated you need to fix your Dialog building process. See below:
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();
OTHER TIPS
First define spinner, then define the adapter. There after see where the error occurs. I guess error wont occur.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow