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 8990933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:36:49+00:00 2026-06-15T22:36:49+00:00

I am a Beginner. I have a problem with a spinner. I want to

  • 0

I am a Beginner. I have a problem with a spinner. I want to create a spinner by using onItemSelected method and to display that selected spinner item in TextView

But my program is not running in emulator. it says “Program stopped”..
what was my mistake in below program. Pls help me.. Thanks U..

My SpinOnItemActivity.java

package spinner.onitemsel;
import android.app.Activity;
  import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.Spinner;
 import android.widget.TextView;
 import android.view.View.OnClickListener;

  public class SpinOnItemActivity extends Activity  
  {
    Spinner sp= (Spinner) findViewById(R.id.spin);;

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.spinner_array , android.R.layout.simple_spinner_item);




            public void onCreate(Bundle savedInstanceState)
            {           
                      super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_spin_on_item );

                    sp.performClick();

            }
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {    
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                sp.setAdapter(adapter);

                String Name = (String) sp.getSelectedItem();

                TextView tview1 = (TextView)findViewById(R.id.testtxt );
                tview1.setText(Name);

            }

 }

My activity_spin_on_item.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SpinActivity" >

<Spinner
    android:id="@+id/spin"
    android:layout_width="149dp"
    android:layout_height="66dp"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/testtxt"
    android:layout_gravity="top" />


<TextView
    android:id="@+id/testtxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/testtxt2"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="44dp"
    android:gravity="bottom"
    android:text="Spin Txt" />

 </RelativeLayout>

My logcat Report

12-10 09:58:40.988: W/Trace(837): Unexpected value from nativeGetEnabledTags: 0
12-10 09:58:41.168: W/Trace(837): Unexpected value from nativeGetEnabledTags: 0
12-10 09:58:41.198: W/Trace(837): Unexpected value from nativeGetEnabledTags: 0
12-10 09:58:41.878: D/dalvikvm(837): GC_CONCURRENT freed 51K, 6% free 2830K/2996K, paused 12ms+3ms, total 56ms
12-10 09:58:41.878: D/dalvikvm(837): WAIT_FOR_CONCURRENT_GC blocked 25ms
12-10 09:58:41.968: D/AndroidRuntime(837): Shutting down VM
12-10 09:58:41.968: W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-10 09:58:41.988: E/AndroidRuntime(837): FATAL EXCEPTION: main
12-10 09:58:41.988: E/AndroidRuntime(837): java.lang.RuntimeException: Unable to start activity ComponentInfo{spinner.onitemsel/spinner.onitemsel.SpinOnItemActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:137)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:5039)
12-10 09:58:41.988: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 09:58:41.988: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:511)
12-10 09:58:41.988: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-10 09:58:41.988: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-10 09:58:41.988: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)
12-10 09:58:41.988: E/AndroidRuntime(837): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:899)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.widget.ListPopupWindow.show(ListPopupWindow.java:603)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.widget.Spinner$DropdownPopup.show(Spinner.java:981)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.widget.Spinner.performClick(Spinner.java:609)
12-10 09:58:41.988: E/AndroidRuntime(837):  at spinner.onitemsel.SpinOnItemActivity.onCreate(SpinOnItemActivity.java:35)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.Activity.performCreate(Activity.java:5104)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-10 09:58:41.988: E/AndroidRuntime(837):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-10 09:58:41.988: E/AndroidRuntime(837):  ... 11 more
12-10 10:03:42.109: I/Process(837): Sending signal. PID: 837 SIG: 9

My Edited Code: Pls Check it out..

 package spinner.onitemsel;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Spinner;
 import android.widget.TextView;

 public class SpinOnItemActivity extends Activity  
 {      
    ArrayAdapter<CharSequence> adapter;
    Spinner sp;
    TextView tview1;
    int length;

            public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_spin_on_item );

                sp= (Spinner) findViewById(R.id.spin);


                adapter = ArrayAdapter.createFromResource(this, R.array.spinner_array ,           android.R.layout.simple_spinner_item);
                     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                sp.setAdapter(adapter);
                sp.performClick();       
            }
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {    
                String Name = (String) sp.getSelectedItem();
                tview1 = (TextView)findViewById(R.id.testtxt );
                tview1.setText(Name);

            }

 }
  • 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-06-15T22:36:50+00:00Added an answer on June 15, 2026 at 10:36 pm

    Actually your Spinner sp is null, So when code line sp.performClick(); is executed in onCreate() the application crashes with Null Pointer Exception.

    Because you are defining Spinner sp in above onCreate() so android doesn’t find the Spinner sp from Layout Xml file. It should be after setContentView() so android nut shell has a reference of that Spinner for particular Activity.

    Solution:

    Put these lines,

    Spinner sp = (Spinner) findViewById(R.id.spin);;
    
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.spinner_array , android.R.layout.simple_spinner_item);
    

    in onCreate() of Activity after setContentView(R.layout.activity_spin_on_item );

    Something like,

    public void onCreate(Bundle savedInstanceState)
    {           
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_spin_on_item );
    
     Spinner sp= (Spinner) findViewById(R.id.spin);;
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this, R.array.spinner_array , android.R.layout.simple_spinner_item);
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     sp.setAdapter(adapter);
    
     sp.performClick();
    }
    

    Update:2

    From your activity_spin_on_item.xml File.

    Look at Parent Tag,

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SpinActivity" >
    

    In this the attribute

    tools:context=".SpinActivity"
    

    should be tools:context=".SpinOnItemActivity"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that might be a simple one, but can't find a
I am a beginner. I want to use pyopengl in linux but I have
I am a beginner at using XSL and XML. I have a problem with
I have a jquery problem, I'm a beginner at this! I want to apply
I am trying use Thread but i have some problem (I am beginner at
Good times for everyone. I have such problem, feeling that it's simple, but due
I'm a beginner in Java programming language, and I have a problem that I
Welcome. I have a problem. I want to do dynamic wallpaper so that every
i am a beginner and i have a problem : this code doesnt compile
I am beginner in ruby + rails. I have problem to get values in

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.