新的AdapterView.OnItemSelectedListener(){}类型必须实现继承的抽象方法错误。日食

时间:2013-04-25 00:17:17

标签: android eclipse

我是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)

的错误如下:

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

有人可以开导我吗?任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:3)

void需要用小写v。

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

而不是

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

您需要更改

s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()