so im really new to the world of android and im trying to code some very basic stuff.I would like to know whats the code to display and make multiple spinner works; so far I did this but I get an error "The type new AdapterView.OnItemSelectedListener(){} must implement the 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 just trying to make 2 spinner(and more after) to display and works at the same time.I also gets these errors when launching it 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 down VM
10-12 15:39:20.269: W/dalvikvm(831): threadid=1: thread exiting with 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 to start activity ComponentInfo{com.example.spinnertest/com.example.spinnertest.MainActivity}: java.lang.NullPointerException
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.os.Looper.loop(Looper.java:137)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-12 15:39:20.279: E/AndroidRuntime(831): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 15:39:20.279: E/AndroidRuntime(831): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 15:39:20.279: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-12 15:39:20.279: E/AndroidRuntime(831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-12 15:39:20.279: E/AndroidRuntime(831): at 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): at com.example.spinnertest.MainActivity.onCreate(MainActivity.java:40)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.Activity.performCreate(Activity.java:5008)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-12 15:39:20.279: E/AndroidRuntime(831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-12 15:39:20.279: E/AndroidRuntime(831): ... 11 more
Thanks!
You are missing the @Override annotations above the methods of the anonymous classes.
Also you need you must implement both methods for interface to 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) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.