Spinner上的Android Null Pointer异常

时间:2014-01-13 15:06:58

标签: android nullpointerexception spinner android-adapter

代码以下措辞可以让我的微调器更改特定项目的选择,但是当我今天启动Android Studio时它给了我一个NullPointerException:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment())
                .commit();
    }

    final Spinner spinnercurent = (Spinner)findViewById(R.id.spinnerCurrentStation);
    //this line produces the error
    spinnercurent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

            if (id == 0) {
                spinnercurent.setSelection(1);
            } else if (id == 20) {
                spinnercurent.setSelection(21);
            } else if (id == 41) {
                spinnercurent.setSelection(42);
            } else if (id == 63) {
                spinnercurent.setSelection(64);
            } else if (id == 84) {
                spinnercurent.setSelection(85);
            }

            StationName = spinnercurent.getSelectedItem().toString();
            Log.d("Output", StationName);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {

        }

    });

XML:

<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/akt_stat"
            android:id="@+id/textViewNext" />

        <Spinner
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinnerCurrentStation"
            android:spinnerMode="dialog"
            android:entries="@array/ubahn_stationen_alle"
            android:paddingLeft="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/ubahn_richtung"
            android:id="@+id/textViewLinie"
            android:paddingTop="20dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button"
            android:id="@+id/button"
            android:onClick="onButtonLineClick" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/ubahn_station"
            android:id="@+id/textViewRechtsLinks"
            android:paddingTop="20dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button"
            android:id="@+id/button2"
            android:onClick="onButtonStationClick" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/direction"
            android:id="@+id/textView"
            android:paddingTop="20dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button"
            android:id="@+id/button3"
            android:onClick="onButtonDirectionClick" />
    </LinearLayout>
</ScrollView>
</RelativeLayout>

这是LogCat消息

Process: at.trumpstuff.parkour, PID: 4260
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.trumpstuff.parkour/at.trumpstuff.parkour.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at at.trumpstuff.parkour.MainActivity.onCreate(MainActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)

1 个答案:

答案 0 :(得分:0)

spinnerCurrentStation具有正确的id作为findView方法的参数。这可能是一个参考错误。尝试清理项目并重新创建引用。