Android Spinner setAdapter()对空对象引用异常

时间:2016-08-25 07:52:37

标签: android nullpointerexception android-widget android-spinner android-dialog

我的目标是动态地向微调器添加项目,我想在对话框(而不是活动类)中执行此操作,但是我得到了这样的例外java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference

DisKapiList是我的列表,其中包含要添加到微调器的项目。

这是我的代码;

spBBKapiNo = (Spinner)dialog.findViewById(UIUtils.getResourceId(wrapper.getmActiviy(), "spBBKapiNo", "id"));

disKapList = yapi.getNumaratajList();
    List <SpinnerObject> lables = getAllLabels();
    ArrayAdapter<SpinnerObject> dataAdapter = new ArrayAdapter<SpinnerObject>(wrapper.getmActiviy(),android.R.layout.simple_spinner_dropdown_item, lables);
    dataAdapter.setDropDownViewResource(UIUtils.getResourceId(wrapper.getmActiviy(), "spBBKapiNo", "id"));
    spBBKapiNo.setAdapter(dataAdapter);

getAllLabels()方法是;

public List <SpinnerObject> getAllLabels(){
    List < SpinnerObject > labels = new ArrayList < SpinnerObject > ();

    for (Numarataj object : disKapList) {
        labels.add ( new SpinnerObject ( object.getId() , object.getKapiNo() ));
    }

    return labels;
}

布局中的微调框字段;

<Spinner
                android:id="@+id/spBBKapiNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:entries="@array/empty_array"
                android:layout_marginLeft="30dp" />

编辑;

现在我可以在dilaog上看到我的SpinnerList,但是当我在clinner中查看其他选项时,程序会关闭资源$ NotFoundException:资源ID#0x7f0f0069类型#0x12无效。

1 个答案:

答案 0 :(得分:0)

已解决!

List <SpinnerObject> lables = getAllLabels();
        ArrayAdapter<SpinnerObject> dataAdapter = new ArrayAdapter<SpinnerObject>(wrapper.getmActiviy(),android.R.layout.simple_spinner_dropdown_item, lables);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spBBKapiNo.setAdapter(dataAdapter);