如何向Spinner添加选项?

时间:2014-07-24 17:33:20

标签: java android android-spinner

所以,我的布局中有一个微调器设置,我已将这段代码实现到strings.xml中:

 <string-array name="priority_array">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
</string-array>

然后我将此代码块放入onCreate():

 super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_info);

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.priority_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

但是,由于某些原因,我的应用程序崩溃了,日志猫指向这行代码,说NullPointerException:

 spinner.setAdapter(adapter);

spinner1不为null,适配器不为null,并且spinner不为null,所以我不知道可能是什么问题。然而,当我注释掉这一行代码时,应用程序完全正常,所以我知道这行与我的应用程序崩溃有关。

这是xml代码段,所以每个人都知道spinner1存在:

 <Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp" />

logcat的:

07-24 13:52:07.728: D/ActivityThread(24892): setTargetHeapUtilization:0.25
07-24 13:52:07.728: D/ActivityThread(24892): setTargetHeapIdealFree:8388608
07-24 13:52:07.728: D/ActivityThread(24892): setTargetHeapConcurrentStart:2097152
07-24 13:52:08.108: D/AbsListView(24892): Get MotionRecognitionManager
07-24 13:52:08.659: I/Adreno200-EGLSUB(24892): <ConfigWindowMatch:2136>: Format     RGBA_8888.
07-24 13:52:08.709: E/(24892): <s3dReadConfigFile:75>: Can't open file for reading
07-24 13:52:08.709: E/(24892): <s3dReadConfigFile:75>: Can't open file for reading
07-24 13:52:10.781: D/AbsListView(24892): Get MotionRecognitionManager
07-24 13:52:10.921: I/Adreno200-EGLSUB(24892): <ConfigWindowMatch:2136>: Format     RGBA_8888.
07-24 13:52:11.122: E/SpannableStringBuilder(24892): SPAN_EXCLUSIVE_EXCLUSIVE spans     cannot have a zero length
07-24 13:52:11.122: E/SpannableStringBuilder(24892): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-24 13:52:13.254: W/dalvikvm(24892): threadid=1: thread exiting with uncaught   exception (group=0x41bf4438)
07-24 13:52:13.264: E/AndroidRuntime(24892): FATAL EXCEPTION: main
07-24 13:52:13.264: E/AndroidRuntime(24892): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.daymanager/com.example.daymanager.ActivityInfo}: java.lang.NullPointerException
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.os.Looper.loop(Looper.java:137)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.ActivityThread.main(ActivityThread.java:4953)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at java.lang.reflect.Method.invoke(Method.java:511)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at dalvik.system.NativeStart.main(Native Method)
07-24 13:52:13.264: E/AndroidRuntime(24892): Caused by: java.lang.NullPointerException
07-24 13:52:13.264: E/AndroidRuntime(24892):    at com.example.daymanager.ActivityInfo.onCreate(ActivityInfo.java:31)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.Activity.performCreate(Activity.java:5160)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-24 13:52:13.264: E/AndroidRuntime(24892):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-24 13:52:13.264: E/AndroidRuntime(24892):    ... 11 more

1 个答案:

答案 0 :(得分:0)

这是因为你的微调器是空的。我想你忘了加载布局。在findViewById

之前调用setContentView(R.layout .----)