[英]Using multiple spinner(android)
因此,我對android系統真的很陌生,並嘗試編寫一些非常基本的東西。我想知道要顯示什么並使多個微調器工作的代碼; 到目前為止,我做到了,但是出現錯誤“新的AdapterView.OnItemSelectedListener(){}類型必須實現繼承的抽象方法AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)”。此處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>
和繼承人的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
謝謝!
您缺少匿名類方法上方的@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) {
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.