java - Using multiple spinner(android) -



java - Using multiple spinner(android) -

so im new world of android , im trying code basic stuff.i know whats code display , create multiple spinner works; far did error "the type new adapterview.onitemselectedlistener(){} must implement inherited abstract method adapterview.onitemselectedlistener.onnothingselected(adapterview)".heres xml:

<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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.spinnertest.mainactivity" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content"> </textview> <edittext android:id="@+id/edittext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/spinner3" android:layout_below="@+id/textview1" android:layout_marginleft="68dp" android:ems="10" android:inputtype="number" > <requestfocus /> </edittext> <spinner android:id="@+id/spinner2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/spinner3" android:layout_torightof="@+id/edittext1" android:entries="@array/type" /> <spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/edittext1" android:layout_alignparentleft="true" android:entries="@array/produits" /> <edittext android:id="@+id/edittext2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_below="@+id/textview1" android:ems="10" android:inputtype="number" /> <edittext android:id="@+id/edittext3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext1" android:layout_toleftof="@+id/edittext2" android:ems="10" android:inputtype="number" /> <edittext android:id="@+id/edittext4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_below="@+id/spinner2" android:ems="10" android:inputtype="number" /> <spinner android:id="@+id/spinner3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/edittext4" android:layout_below="@+id/spinner4" /> <spinner android:id="@+id/spinner4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/edittext3" android:layout_torightof="@+id/edittext1" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/edittext3" android:layout_alignparentbottom="true" android:layout_marginbottom="23dp" android:text="textview" /> <spinner android:id="@+id/spinner5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/edittext4" android:layout_torightof="@+id/button1" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textview2" android:layout_marginleft="93dp" android:layout_torightof="@+id/textview2" android:text="button" /> </relativelayout>

and heres java code:

package com.example.spinnertest; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.spinner; import android.widget.textview; public class mainactivity extends activity { spinner s1,s2; textview txt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); s1 = (spinner) findviewbyid(r.id.spinner1); txt = (textview) findviewbyid(r.id.textview2); s1.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { string val = s1.getselecteditem().tostring(); txt.settext(val); } @override public void onnothingselected(adapterview<?> adapterview) { } }); s2.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { string val = s2.getselecteditem().tostring(); txt.settext(val); } @override public void onnothingselected(adapterview<?> adapterview) { } });}}

so im trying create 2 spinner(and more after) display , works @ same time.i gets these errors when launching on emulated device:

10-12 15:39:19.388: e/trace(831): error opening trace file: no such file or directory (2) 10-12 15:39:20.269: d/androidruntime(831): shutting downwards vm 10-12 15:39:20.269: w/dalvikvm(831): threadid=1: thread exiting uncaught exception (group=0x40a13300) 10-12 15:39:20.279: e/androidruntime(831): fatal exception: main 10-12 15:39:20.279: e/androidruntime(831): java.lang.runtimeexception: unable start activity componentinfo{com.example.spinnertest/com.example.spinnertest.mainactivity}: java.lang.nullpointerexception 10-12 15:39:20.279: e/androidruntime(831): @ android.app.activitythread.performlaunchactivity(activitythread.java:2059) 10-12 15:39:20.279: e/androidruntime(831): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2084) 10-12 15:39:20.279: e/androidruntime(831): @ android.app.activitythread.access$600(activitythread.java:130) 10-12 15:39:20.279: e/androidruntime(831): @ android.app.activitythread$h.handlemessage(activitythread.java:1195) 10-12 15:39:20.279: e/androidruntime(831): @ android.os.handler.dispatchmessage(handler.java:99) 10-12 15:39:20.279: e/androidruntime(831): @ android.os.looper.loop(looper.java:137) 10-12 15:39:20.279: e/androidruntime(831): @ android.app.activitythread.main(activitythread.java:4745) 10-12 15:39:20.279: e/androidruntime(831): @ java.lang.reflect.method.invokenative(native method) 10-12 15:39:20.279: e/androidruntime(831): @ java.lang.reflect.method.invoke(method.java:511) 10-12 15:39:20.279: e/androidruntime(831): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 10-12 15:39:20.279: e/androidruntime(831): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 10-12 15:39:20.279: e/androidruntime(831): @ dalvik.system.nativestart.main(native method) 10-12 15:39:20.279: e/androidruntime(831): caused by: java.lang.nullpointerexception 10-12 15:39:20.279: e/androidruntime(831): @ com.example.spinnertest.mainactivity.oncreate(mainactivity.java:40) 10-12 15:39:20.279: e/androidruntime(831): @ android.app.activity.performcreate(activity.java:5008) 10-12 15:39:20.279: e/androidruntime(831): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1079) 10-12 15:39:20.279: e/androidruntime(831): @ android.app.activitythread.performlaunchactivity(activitythread.java:2023) 10-12 15:39:20.279: e/androidruntime(831): ... 11 more

thanks!

you missing @override annotations above methods of anonymous classes.

also need must implement both methods interface work.

s1.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { string val = s1.getselecteditem().tostring(); txt.settext(val); } @override public void onnothingselected(adapterview<?> adapterview) { } }); s2.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { string val = s2.getselecteditem().tostring(); txt.settext(val); } @override public void onnothingselected(adapterview<?> adapterview) { } });

java android android-layout android-activity android-spinner

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -