使用ArrayAdapter的NullPointerException

时间:2014-10-02 18:13:50

标签: java android arraylist nullpointerexception android-arrayadapter

我正在使用Eclipse,我有一个使用默认" RelativeLayout"布局。我在布局中包含了一个微调器,这里是当前的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="mahana.timestat.InputMenu" >

    <Spinner
        android:id="@+id/activitiesSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

在我的.java中,我找到了微调器,创建了一个arrayList,手动填充它,创建一个适配器并将其设置为微调器的适配器。这是代码:

//find the spinner:
Spinner activitiesSpinner = (Spinner) findViewById(R.id.activitiesSpinner);
//create an arrayList:
ArrayList<String> activities = new ArrayList<String>();
//clear it and insert two strings:
activities.clear();
activities.add("First");
activities.add("Second");
//create an adapter (I expect the error to be somewhere in here)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.id.activitiesSpinner, activities);
//and finally set the adapter for the spinner
activitiesSpinner.setAdapter(adapter);

当我使用adapter.getCount()方法时,它会显示正确的数字(2)。 当我在最后一行注释时,我设置了适配器,它运行正常并在我的设备上显示一个空的微调器。 当我离开最后一行并运行它时,它会在nullPointerException上崩溃。这是跟踪:

10-02 19:50:05.645: E/AndroidRuntime(21432): FATAL EXCEPTION: main
10-02 19:50:05.645: E/AndroidRuntime(21432): Process: mahana.timestat, PID: 21432
10-02 19:50:05.645: E/AndroidRuntime(21432): java.lang.NullPointerException
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.Spinner.onMeasure(Spinner.java:504)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.View.measure(View.java:16538)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.View.measure(View.java:16538)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.View.measure(View.java:16538)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
10-02 19:50:05.645: E/AndroidRuntime(21432): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.View.measure(View.java:16538)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-02 19:50:05.645: E/AndroidRuntime(21432): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2373)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.View.measure(View.java:16538)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1964)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1160)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1342)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5896)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.Choreographer.doFrame(Choreographer.java:544)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.os.Handler.handleCallback(Handler.java:733)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.os.Looper.loop(Looper.java:136)
10-02 19:50:05.645: E/AndroidRuntime(21432): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-02 19:50:05.645: E/AndroidRuntime(21432): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 19:50:05.645: E/AndroidRuntime(21432): at java.lang.reflect.Method.invoke(Method.java:515)
10-02 19:50:05.645: E/AndroidRuntime(21432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-02 19:50:05.645: E/AndroidRuntime(21432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
10-02 19:50:05.645: E/AndroidRuntime(21432): at dalvik.system.NativeStart.main(Native Method)

当我尝试查看这个问题时,我发现人们不小心在他们的arrayList中的某个地方留下了一个空值,但这绝对不是我的问题,因为我手动填充它。 任何关于问题可能出现的想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

因为您使用的是默认simple_spinner_item布局,因此其中包含TextView text1 ID而不是Spinner。所以不要将R.id.activitiesSpinner作为第三个参数传递给ArrayAdapter构造函数传递android.R.id.text1

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                                android.R.layout.simple_spinner_item, 
                                android.R.id.text1, activities);

OR

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                                android.R.layout.simple_spinner_item,activities);