0
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.app.Activity;
import java.lang.String;

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adminsignup);
        spinner dropdown = (Spinner)findViewById(R.id.selectbranch);
        String[] items = new String[]{"CSE", "EC", "IT","EN","EDT","CIVIL","EE","IND","MECH","MBA","MCA","CLUB"};

        ArrayAdapter<String> adapter = new ArrayAdapter< >(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);    
    }   
}

Error is at .setAdapter it comes in red. I have tried all possible means, even tried changing this to this.getActivity() but to no avail.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

Actually, the error is in this line:

spinner dropdown = (Spinner)findViewById(R.id.selectbranch);

because You try to declare "dropdown" variable of "spinner" type, and there is no "spinner" class. There are two solutions, one is to declare variable dropdown as Spinner type, like this:

Spinner dropdown = (Spinner)findViewById(R.id.selectbranch);

Or, You can use private variable "spinner" that You declare at the begining of MainActivity class, then code should look like this:

    spinner = (Spinner)findViewById(R.id.selectbranch);
    String[] items = new String[]{"CSE", "EC", "IT","EN","EDT","CIVIL","EE","IND","MECH","MBA","MCA","CLUB"};

    ArrayAdapter<String> adapter = new ArrayAdapter< >(this, android.R.layout.simple_spinner_dropdown_item, items);
    spinner.setAdapter(adapter);

Both solution will work, but if you planning to use this adapter later in the code, delcaring private variable is better option. Also, you don't need to use "new String[]" when you define array with {}, and you don't need to use "<>" after "new ArrayAdapter".