Spinner在android中不起作用

时间:2011-08-02 06:26:34

标签: android spinner

Spinner无法正常工作。这是我的代码:

public class second extends Activity{   
    Spinner spin;
    String[] str;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);        
        spin = (Spinner) findViewById(R.id.spin);
        str = new String[] {"bike","car"};      
        spin.setAdapter(new ArrayAdapter<CharSequence>(this, android.R.layout.simple_dropdown_item_1line, str));    

        spin.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapter, View view, int pos,
                long id) {
            // TODO Auto-generated method stub

            String data= spin.getItemAtPosition(pos).toString();
            Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
               }    
        });
    }
}

4 个答案:

答案 0 :(得分:5)

问题出在这里,

 spin.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> adapter, View view, int pos,
            long id) {
        // TODO Auto-generated method stub

        String data= spin.getItemAtPosition(pos).toString();
        Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
    }

});

尝试按如下方式替换它,

 spin.OnItemSelectedListener (new OnItemSelectListener() {

    public void onItemSelect(AdapterView<?> adapter, View view, int pos,
            long id) {
        // TODO Auto-generated method stub

        String data= spin.getItemAtPosition(pos).toString();
        Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
    }

});

答案 1 :(得分:5)

你犯的唯一错误是你设置了不同的事件监听器..

收听者OnItemClickListener适用于ListView ..

对于Spinner,您必须设置OnItemSelectedListener ..

尝试将OnItemClickListener替换为OnItemSelectedListener,例如:

spin.OnItemSelectedListener (new OnItemSelectedListener() {

    public void onItemClick(AdapterView<?> adapter, View view, int pos,
            long id) {
        // TODO Auto-generated method stub

        String data= spin.getItemAtPosition(pos).toString();
        Toast.makeText(second.this, data, Toast.LENGTH_SHORT).show();
    }

});

答案 2 :(得分:5)

这是你的oncreate()方法中的问题:

spin = (Spinner) findViewById(R.id.spin);

您可以将结果分配给spinner,这是一个局部变量/引用。 main.spinner类字段仍为null。这就是你在calculate()方法中有NullPointerException的原因。

将以上行替换为:

this.spin = (Spinner) findViewById(R.id.spin);

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

答案 3 :(得分:0)

试试这个

 spinner = (Spinner) findViewById(R.id.spinner);
 List<String> categories = new ArrayList<String>();
    categories.add("--Qualification--");
    categories.add("High School");
    categories.add("Higher Secondary/PUC");
    categories.add("Diploma");
    categories.add("Degree");
    categories.add("Master Degree");


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    R.layout.spinnertext, categories);
    adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
    spinner.setAdapter(adapter);

Check Out on this link