我在使用一周中的几天填充我的微调器时遇到了问题。
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);
}
}
导致应用程序崩溃的setAdapter行。我收到以下错误
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
昨天工作正常。我无法弄清楚我是否可以改变一些东西,或者我是否有日食问题。
答案 0 :(得分:35)
Nambari解释了为什么您收到NullPointerException以及如何正确使用findViewById()
。
我想指出一种更简单的方法来设置使用默认设置的Spinner,只需将其添加到您的XML中:
android:entries="@array/days_of_week"
您可以在onCreate()
中删除对适配器的所有引用。
答案 1 :(得分:8)
super.onCreate(savedInstanceState);
setContentView(yourView);
day = (Spinner)findViewById(R.id.week_spinner);
在从xml获取微调器之前,您忘记设置视图。如果您未设置视图,findViewById
会返回null
。
对null
引用的任何操作都会导致NullPointerException
。