Reputation: 29
I've looked at several threads and none of the suggestions made in there seem to fix the problem that I am having. I have 2 spinners in the same activity. The value that I select from one spinner is meant to populate the other one, and then I want to select a value from that second spinner. It works all the way up until I go to click on the second spinner after it has been dynamically populated, and then the application crashes. I'm not sure what else to do. I will post the relevant code that I have as well as the error message I am getting.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bodyregionandpart_layout);
bodyRegion = (Spinner) findViewById(R.id.bodyregion);
// Create an ArrayAdapter using the string array and a default spinner layout
bodyRegionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bodyregion);
// Specify the layout to use when the list of choices appears
bodyRegionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
bodyRegion.setAdapter(bodyRegionAdapter);
//Sets the spinner to listen when an item is selected
bodyRegion.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id)
{
if(parent.getId() == R.id.bodyregion)
{
switch(pos)
{
case 1:
FillSpinner(Head);
Log.d("bodyregion", "This is Head");
break;
}
}
else if(parent.getId() == R.id.bodypart)
{
Log.d("bodypart", "This is body part: " + (String) parent.getItemAtPosition(pos));
}
}
public void onNothingSelected(AdapterView<?> parent)
{
// Another interface callback
}
public void FillSpinner(String[] a)
{
//Fills array with contents of a
for(int i = 0; i < a.length; i++)
{
array[i] = a[i];
}
bodyPart = (Spinner) findViewById(R.id.bodypart);
// Create an ArrayAdapter using the string array and a default spinner layout
bodyPartAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, array);
// Specify the layout to use when the list of choices appears
bodyPartAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
bodyPart.setAdapter(bodyPartAdapter);
bodyPart.setOnItemSelectedListener(this);
}
And then this is the error message that I am receiving:
01-21 22:10:29.616: D/bodyregion(2855): This is Head
01-21 22:10:29.715: D/bodypart(2855): This is body part: Choose Body Part
01-21 22:10:31.385: D/AndroidRuntime(2855): Shutting down VM
01-21 22:10:31.385: W/dalvikvm(2855): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-21 22:10:31.415: E/AndroidRuntime(2855): FATAL EXCEPTION: main
01-21 22:10:31.415: E/AndroidRuntime(2855): java.lang.NullPointerException
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:725)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:721)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.widget.Spinner.measureContentWidth(Spinner.java:669)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.widget.Spinner$DropdownPopup.show(Spinner.java:948)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.widget.Spinner.performClick(Spinner.java:597)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.view.View$PerformClick.run(View.java:16966)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.os.Handler.handleCallback(Handler.java:615)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.os.Looper.loop(Looper.java:137)
01-21 22:10:31.415: E/AndroidRuntime(2855): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-21 22:10:31.415: E/AndroidRuntime(2855): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 22:10:31.415: E/AndroidRuntime(2855): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 22:10:31.415: E/AndroidRuntime(2855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-21 22:10:31.415: E/AndroidRuntime(2855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-21 22:10:31.415: E/AndroidRuntime(2855): at dalvik.system.NativeStart.main(Native Method)
The second log message just shows the first choice in the spinner when it is created. Once I click it to select something else it crashes. Any help/suggestions are appreciated. Thanks.
Upvotes: 1
Views: 168
Reputation: 21191
Do small change..check if the length of the array is greater than 0
then only set adapter to the second spinner
public void FillSpinner(String[] a)
{
//Fills array with contents of a
for(int i = 0; i < a.length; i++)
{
array[i] = a[i];
}
if(array.length>0)
{
bodyPart = (Spinner) findViewById(R.id.bodypart);
// Create an ArrayAdapter using the string array and a default spinner layout
bodyPartAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, array);
// Specify the layout to use when the list of choices appears
bodyPartAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
bodyPart.setAdapter(bodyPartAdapter);
bodyPart.setOnItemSelectedListener(this);
}
}
Upvotes: 1