4

我是 android 编程的初学者,但还遇到了另一个问题。我正在制作一个简单的微调器应用程序。这是代码。

package net.learn2develop.Basicviews6;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class BasicViews6Activity extends Activity {
String[] presidents;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    presidents =
            getResources().getStringArray(R.array.presidents_array);
    Spinner s1 = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, presidents);

    s1.setAdapter(adapter);
    s1.setOnItemSelectedListener(new OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> arg0,
                View arg1, int arg2, long arg3)
        {
            int index = arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(),
                    "You have selected item : " + presidents[index],
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public Void onNothingdSelected(AdapterView<?> arg0) { }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.basic_views6, menu);
    return true;
}

}

但它不会让我运行它,因为这些代码行:

        s1.setOnItemSelectedListener(new OnItemSelectedListener()
public Void onNothingdSelected(AdapterView<?> arg0)

有如下错误:

new AdapterView.OnItemSelectedListener(){} 类型必须实现继承的抽象方法 AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)

有人可以启发我吗?任何帮助都感激不尽!

4

3 回答 3

3

void 需要写成小写的 v。

public void onNothingdSelected(AdapterView<?> arg0) { }

代替

public Void onNothingdSelected(AdapterView<?> arg0) { }
于 2013-04-25T00:22:59.150 回答
2

你的方法说onNothingdSelected()但它应该是onNothingSelected()。改变它;你拼错了。

于 2013-04-25T00:23:57.750 回答
1

你需要改变

s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
于 2013-04-25T00:35:08.430 回答