I'm trying to set my spinner position on activity entry but i get this msg on logcat
Attempt to invoke virtual method 'void android.widget.Spinner.setSelection(int)' on a null object reference
I have and array delcared as:
<string-array name="genres">
<item>Action</item>
<item>Animation</item>
</string-array>
in the activity i have
movieGenre = (Spinner) findViewById(R.id.dd_genre);
Log.e("ListView set Item", String.valueOf(movieItem.getaNGenre()));
movieGenre.setSelection(movieItem.getaNGenre());
logcat shows me the number correctly:
05-09 12:46:35.624 4940-4940/simpleapps.movierandomizer E/ListView set Itemīš 3
but when i set the position, it wont accept the number. I dont understand why it says "on a null object reference" when i have declared it.
Did you populate the spinner with the items (probably the string array in this case?)
Also, if the item number (3 in this case) that you are trying to set,is more than the spinner items, it will throw a run time error.
You have to set the adapter for the spinner first. After setting up the adapter it won't throw the exception that you have mentioned.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_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);
Related
I'm working on an android project. I am using spinner to show a drop down list. I want to make some items disable on the basis of some conditions. Following is code for what I have tried:
ArrayAdapter<CharSequence> statusArray = ArrayAdapter.createFromResource(getApplicationContext(), R.array.status_array, android.R.layout.simple_spinner_item);
statusArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Disable item at position 3
statusArray.setSelection(2, false);
// Set Adapter for Spinner
statusSpinner.setAdapter(statusArray);
This is not the exact code but similar to original one. I'm new to android. Any help would be appreciated.
Whenever an selection occur in spinner on condition call setEnabled method above setAdapter method
ArrayAdapter<CharSequence> statusArray = ArrayAdapter.createFromResource(getApplicationContext(), R.array.status_array, android.R.layout.simple_spinner_item);
statusArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Disable item at position 3
statusArray.setSelection(2, false);
//Set enable disable as follow
statusSpinner.setEnabled(true/false)
statusSpinner.setClickable(true/false)
// Set Adapter for Spinner
statusSpinner.setAdapter(statusArray);
I have got two spinners that receive a default value as a variable from another view. The first one is working fine but the second one not (it shows position 0 as the default value). Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
l_target and l_source are the variables and they are correctly sent from the previous view to this one.
I would be grateful if you could find what and why is failing.
Thank you for your time.
I have an activity that recieves two variables called l_source and l_destination and I want to set the default values of two spinners equal to those variables. To do so, I have made a search and I understand that I need to get the position of the value of the variable (will give a number) and then set the new position of the spinner. The problem is that I am trying to use getAdapterPosition and I am sent Cannot resolve method getAdapterPosition(l_source).
EDIT
I finally managed to do it, but it is only working in one spinner. Have you got any idea of why? Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
Thank you for your time.
get position of selected item is spinner by int spinnerPosition = spinner.getSelectedItemPosition();
I am moving from one activity to other activity in that oncreate method i am calling a webserive and get data into arraylist and assign to adapter like this
Arrayadapter<String> adapter=new Arrayadapter<String>(this,android.R.id.layout_simpleid, arraylistobj);
then
Spinner s = (Spinner) findViewById(R.id.something);
s.setAdapter(adapter);
but it is giving me a NullPointerException near setAdapter(adapter).
I'm trying to populate the spinner, but the application crashes on spiner.setAdapter(adapter)...
final Context c=this;
ArrayList<CountryItem> countriesArray = GetCountries1();
ArrayAdapter<CountryItem> adapter = new ArrayAdapter<CountryItem>(
c,android.R.layout.simple_spinner_item, countriesArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner spiner=(Spinner)findViewById(R.id.spinner1);
spiner.setAdapter(adapter);
I can't find any errors here, and eclipse debug mode does not show anything usefull...
Here is the example from google...is it so much different than my code?
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
What error do you get?
Did you check if the countriesArray has elements or if it is empty?
Is it working when you use, e.g., a simple string array? If yes, then the problem lies with the CountryItem objects.
Other than that, the only difference I see is that you pass the context as a final variable, but I don't know if this causes the error. Try to pass just this and see if it works.