Spinner error on SetOnItemSelectedListener

2019-05-05 02:39发布

问题:

I have a spinner, and I do this

 public class Settings extends Activity {

String[] items = new String[] {"English", "Deutsch", "日語"};
 SharedPreferences prefs;
  SharedPreferences.Editor prefEditor;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.settings);
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
 spinner.setOnItemSelectedListener (new OnItemSelectedListener()  {


    });

There's syntax error suggesting OnItemSelectedListener cannot resolved into type. The method setOnItemSelectedListener is not applicable

回答1:

This is common problem on eclipse. Just add it manually:

import android.widget.AdapterView.OnItemSelectedListener;


回答2:

Fix your imports. Press ctrl+shift+o if on Eclipse.