I have an android app, written in java that contains, amongst other things, a spinner, shown in the layout.xml as follows:
<Spinner
android:id="@+id/sprDeviceType"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
It is initialised in the activity class as follows:
public class AddDevice extends Activity
{
private Spinner deviceTypeSpinner;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add); //Set the layout
populateDeviceTypeSpinner();
}
private void populateDeviceTypeSpinner()
{
String device_type_spinner[];
device_type_spinner = new String[2];
device_type_spinner[0] = "FTP";
device_type_spinner[1] = "QAF";
deviceTypeSpinner = (Spinner) findViewById(R.id.sprDeviceType);
ArrayAdapter deviceTypeArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, device_type_spinner);
deviceTypeSpinner.setAdapter(deviceTypeArrayAdapter);
}
}
However, when I run the program, it opens the activity fine, but when I click the spinner to get the drop down items, it crashes the program, throwing a NullPointerException.
Anyone see what I’m doing wrong?
The LogCat shows:
08-14 14:57:02.034: D/AndroidRuntime(1612): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
08-14 14:57:02.034: D/AndroidRuntime(1612): CheckJNI is OFF
08-14 14:57:02.074: D/AndroidRuntime(1612): --- registering native functions ---
08-14 14:57:05.004: D/AndroidRuntime(1612): Shutting down VM
08-14 14:57:05.014: I/AndroidRuntime(1612): NOTE: attach of thread 'Binder Thread #3' failed
08-14 14:57:05.184: D/AndroidRuntime(1637): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<
08-14 14:57:05.184: D/AndroidRuntime(1637): CheckJNI is OFF
08-14 14:57:05.224: D/AndroidRuntime(1637): --- registering native functions ---
08-14 14:57:05.524: D/AndroidRuntime(1637): Shutting down VM
08-14 14:57:05.544: I/AndroidRuntime(1637): NOTE: attach of thread 'Binder Thread #3' failed
I changed from using an array I programmed in, to using an array saved in a values xml.