14

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.

4

2 回答 2

41

Nambari 解释了您收到 NullPointerException 的原因以及如何正确使用findViewById().

我想指出一种更简单的方法来设置使用默认设置的 Spinner,只需将其添加到您的 XML 中:

android:entries="@array/days_of_week"

您可以删除所有对您的适配器的引用onCreate()

于 2012-11-05T21:35:25.407 回答
10
super.onCreate(savedInstanceState);
setContentView(yourView);
day = (Spinner)findViewById(R.id.week_spinner);

在从 xml 获取微调器之前,您忘记设置视图。如果您不设置视图,则findViewById返回null.

对引用的任何操作都会null导致NullPointerException.

于 2012-11-05T21:29:49.077 回答