0

我正在尝试在微调器上使用这个适配器,它给出了空指针异常,请给你有价值的回应

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, R.array.select_action_arrays);

在我的 string.xml 文件中

<string-array name="select_action_arrays">
        <item>alert1</item>
        <item>alert2</item>
        <item>alert3</item>
        <item>alert4</item>
        <item>alert5</item>
    </string-array>

这是完整的代码行

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.select_action_arrays));
        spinnerSelectAction.setAdapter(adapter);
        spinnerSelectAction = (Spinner)findViewById(R.id.spinnerSelectAction);
        spinnerSelectAction.setAdapter(adapter);
        spinnerSelectAction.setOnItemSelectedListener(new OnItemSelectedListener() {
            int count=0;
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                if(count >= 1){
                Toast.makeText(getBaseContext(),"inside on item selected ",Toast.LENGTH_SHORT).show();
                }
                count++;
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {}
        });

spinnerSelectAction 似乎已初始化

4

3 回答 3

2

您似乎没有正确初始化适配器。它应该是这样的:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
     android.R.layout.simple_spinner_item,
     getResources().getStringArray(R.array.select_action_arrays));
于 2012-10-23T12:56:57.550 回答
1

写在下面的代码行

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.select_action_arrays));

代替

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, R.array.select_action_arrays);

也许它会解决你的问题。

于 2012-10-23T12:56:03.103 回答
0

您在从视图中检索微调器之前添加到适配器,前 2 条指令应该是这样的:

spinnerSelectAction = (Spinner)findViewById(R.id.spinnerSelectAction);
spinnerSelectAction.setAdapter(adapter);
于 2014-01-09T13:15:02.167 回答