所以我真的很新的android世界和我试图编写一些非常基本的东西。我想知道什么代码显示和制作多个微调工程;到目前为止,我做了这个,但我得到一个错误“类型新的AdapterView.OnItemSelectedListener(){}必须实现继承的抽象方法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>
和heres java代码:
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) {
}
});}}
所以我只是想让2个微调器(以及更多)在同一时间显示和工作。我在模拟设备上启动时也会出现这些错误:
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
谢谢!
答案 0 :(得分:0)
您缺少匿名类方法上方的@Override注释。
此外,您还需要实现两种接口工作方法。
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) {
}
});