Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7557931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:15:42+00:00 2026-05-30T12:15:42+00:00

Hi i am trying to a populate a spinner with strings using arrayadapter.but i

  • 0

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>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:15:43+00:00Added an answer on May 30, 2026 at 12:15 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to populate a treeview using mvvm but the tree does not
I'm trying to populate the spinner, but the application crashes on spiner.setAdapter(adapter)... final Context
I'm trying to populate a dropdown list inside a repeater, but I'm not being
I am trying to populate a Spinner in an Android dialog. When the user
I am trying to populate a gridview using LayoutInflator.. According to me the code
I'm trying to populate a list of files, and I'm using a custom ArrayAdapter
I am trying to populate a string with a double value using a sprintf
I'm trying to populate a DataTable, to build a LocalReport, using the following: MySqlCommand
I'm trying to populate a dictionary dynamically in a for loop like so: pseudo-code
I'm trying to populate this list with strings from the object data. It comes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.