user1797190 user1797190 - 6 months ago 16
Java Question

android: populate a spinner from strings.xml

I'm having a problem with populating my spinner with days of the week.

layout xml:

<Spinner
android:id="@+id/week_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"/>


strings.xml

<resources>
<string-array name="days_of_week">
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
</string-array>
</resources>


java:

public class AddModule extends Activity {
Spinner day;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
day = (Spinner)findViewById(R.id.week_spinner);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.days_of_week, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
day.setAdapter(adapter);
setContentView(R.layout.activity_add_module);
}
}


It's the setAdapter line that's causing the app to crash. I'm getting the following errors

11-05 21:24:31.235: W/dalvikvm(867): threadid=1: thread exiting with uncaught exception (group=0x40a9a228)
11-05 21:24:31.245: E/AndroidRuntime(867): FATAL EXCEPTION: main
11-05 21:24:31.245: E/AndroidRuntime(867): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cathal.timetable/com.cathal.timetable.AddModule}: java.lang.NullPointerException
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.ActivityThread.access$600(ActivityThread.java:139)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.os.Looper.loop(Looper.java:154)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.ActivityThread.main(ActivityThread.java:4974)
11-05 21:24:31.245: E/AndroidRuntime(867): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 21:24:31.245: E/AndroidRuntime(867): at java.lang.reflect.Method.invoke(Method.java:511)
11-05 21:24:31.245: E/AndroidRuntime(867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-05 21:24:31.245: E/AndroidRuntime(867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-05 21:24:31.245: E/AndroidRuntime(867): at dalvik.system.NativeStart.main(Native Method) 11-05 21:24:31.245: E/AndroidRuntime(867): Caused by: java.lang.NullPointerException 11-05 21:24:31.245: E/AndroidRuntime(867): at com.cathal.timetable.AddModule.onCreate(AddModule.java:18)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.Activity.performCreate(Activity.java:4538)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
11-05 21:24:31.245: E/AndroidRuntime(867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)
11-05 21:24:31.245: E/AndroidRuntime(867): ... 11 more


This was working fine yesterday. I can't figure out if I maybe changed something or if I have an eclipse problem.

Answer
super.onCreate(savedInstanceState);
setContentView(yourView);
day = (Spinner)findViewById(R.id.week_spinner);

You forgot to set view before getting spinner from xml. If you don't set view, findViewById returns null.

Any operation on null reference results in NullPointerException.

Comments