2

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); } } 
4
  • post your logcat error report Commented Dec 10, 2012 at 9:45
  • i have attached my logcat report.. pls help me.. Commented Dec 10, 2012 at 10:14
  • ji.. as ur update.. now i've following 2 errors.. 1.spin cannot be resolved or is not a field 2. testtxt cannot be resolved or is not a field but in R.java.. itz present.. public static final int spin=0x7f070000; public static final int testtxt=0x7f070002; wat to do Commented Dec 10, 2012 at 11:01
  • @ user370305 finally..as ur updated code, i got spinner output.. but textview still not display the selected item from spinner.. pls help me.. Commented Dec 10, 2012 at 11:12

2 Answers 2

2

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"

Sign up to request clarification or add additional context in comments.

7 Comments

i put tat two lines in my onCreate() method after setContentView() as you say.. Still my program is not running.. it says "Program stopped" wat to do..
Stil i hav tat same problem.. i have attached my edited code and logcat .. pls check it out..
ji.. as ur update.. now i've following 2 errors.. spin cannot be resolved or is not a field testtxt cannot be resolved or is not a field but in R.java.. itz present.. public static final int spin=0x7f070000; public static final int testtxt=0x7f070002; wat to do..
finally..as ur updated code, i got spinner output.. but textview still not display the selected item from spinner.. pls help me..
Implement OnItemSelectedListener in your activity. Look at this tutorial developer.android.com/guide/topics/ui/controls/spinner.html . And register spinner for it. like, spinner.setOnItemSelectedListener(this);
|
2
Spinner sp= (Spinner) findViewById(R.id.spin);; ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.spinner_array , android.R.layout.simple_spinner_item); 

will come inside your onCreate() method after setContentView()


Edit: Then

 sp.setAdapter(adapter); TextView tview1 = (TextView)findViewById(R.id.testtxt ); 

will also come in onCreate() method after setContentView()

Edit:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_spin_on_item); Spinner sp = (Spinner) findViewById(R.id.spin); TextView tview1 = (TextView) findViewById(R.id.testtxt); 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(); } public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String Name = (String) sp.getSelectedItem(); tview1.setText(Name); } 

5 Comments

@Shreya Shah i put tat two lines in my onCreate() method after setContentView().. Still my program is not running.. it says "Program stopped" wat to do..
Please post the logcat output in your question, so we can see where the error is.
@ShreyaShah - For TextView its not necessary, OP can put that line after onCreate() once setContentView() called.
@user370305, yes, I know. But its a good practice. So I just mentioned it.
@ShreyaShah Stil i hav tat same problem.. i have attached my edited code and logcat .. pls check it out..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.