Brandon Bachman
Brandon Bachman

Reputation: 33

Android Spinners - Cannot resolve symbol - setOnItemSelectedListener

I'm new to Android programming an I'm trying to implement a spinner, but I'm getting "cannot resolve symbol" errors for setOnItemSelectedListener, setDropDownViewResource, and setAdapter. I'm stumped. I a tried adding more imports, but they didn't do anything.

Here is my code:

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
    public final static String EXTRA_MESSAGE = "com.example.FinalProject.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);

// Create an ArrayAdapter using the string array and a default spinner layout

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.verb_endings_array, android.R.layout.simple_spinner_item);

// Specify the layout to use when the list of choices appears

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// Apply the adapter to the spinner

    spinner.setAdapter(adapter);

    public void onItemSelected(AdapterView<?> parent, View view,
                           int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}    

Upvotes: 1

Views: 2364

Answers (1)

Move this

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);

inside the onCreate Method, BUT declare the spinner outside, (you want for sure work with it later... :))

private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
}

Upvotes: 3

Related Questions