I am trying to add few items in the spinner list. But somehow everytime, I always get the below exception on this–
Cannot use this in a static context
Below is my code
public static void initSpinnerView(Context context, Spinner spinnerView, String prefix, int numItems, int layout) {
//spinnerView = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerView.setAdapter(dataAdapter);
}
}
Can anyone tell me what wrong I am doing here?
This is a Java keyword indicateing the current object. If I am not wrong, this method is placed in an class that extends Activity. Hence,
thisrefers to the Activity object( which is used as Context in this case). And since it is a static method, you cannot used the keywordthisor non-static variables.In order to fix it,change to this line: