0

我使用 Eclipse ADT build:v21.1.0-569685,案例一是来自网站的示例代码,但它会导致错误“类型 new AdapterView.OnItemSelectedListener(){} 必须实现继承的抽象方法 AdapterView.OnItemSelectedListener.onNothingSelected(适配器视图)"

所以我必须使用案例二,但有时案例一还可以,发生了什么?谢谢!

案例一

private void InitSpinner(){
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,
            nameList.toArray(new String[nameList.size()]));
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerFolder.setAdapter(adapter1); 
    spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(MainActivity.this,
                    "cw", 2000).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    }); 
}

案例二

private void InitSpinner(){
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,
            nameList.toArray(new String[nameList.size()]));
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerFolder.setAdapter(adapter1); 
    spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(MainActivity.this,
                    "cw", 2000).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    }); 
}
4

1 回答 1

1

@保罗

参数名称是否必须相同?

不,参数名称不必相同。因此,这两个代码完全相同,并且将以相同的方式工作。

为什么我得到

 The type new AdapterView.OnItemSelectedListener(){} must implement the inherited abstract method AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)

错误?

我猜你一定是从你的其他项目中复制了那部分代码,或者一定是通过某个站点直接从互联网上复制的。在这里,您已经复制了正确的语法,但没有对相同的语法进行正确的导入......

尝试..

Ctrl+Shift+O 组织所有必需的导入...

希望能帮助到你.....

于 2013-05-16T15:18:50.347 回答