繁体   English   中英

使用多个微调器(android)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM