微调器在 Android 中不工作

发布于 2024-11-27 13:33:38 字数 914 浏览 0 评论 0原文

微调器不工作。这是我的代码:

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();
               }    
        });
    }
}

Spinner is not working. Here is my code:

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();
               }    
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青萝楚歌 2024-12-04 13:33:38

您犯的唯一错误是您设置了不同的事件侦听器。

侦听器 OnItemClickListener 用于 ListView

对于 Spinner 您必须设置 < code>OnItemSelectedListener ..

尝试将 OnItemClickListener 替换为 setOnItemSelectedListener,如下所示:

spin.setOnItemSelectedListener (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();
    }

});

The only mistake you made is you have set different event listener..

The Listener OnItemClickListener is for ListView..

For Spinner you have to set OnItemSelectedListener ..

Try replacing OnItemClickListener to setOnItemSelectedListener like :

spin.setOnItemSelectedListener (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();
    }

});
送你一个梦 2024-12-04 13:33:38

问题出在这里,

 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();
    }

});

The problem is here,

 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();
    }

});

Try replacing it as follows,

 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();
    }

});
又怨 2024-12-04 13:33:38

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

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

您将结果分配给 spinner,它是此处的局部变量/引用。 main.spinner 类字段仍然为空。这就是为什么在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);

Here is the problem in your oncreate() method:

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

You assign the result to spinner which is a local variable/reference here. main.spinner class field is still null. This is why you have NullPointerException in calculate() method.

Replace the line above with:

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

ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,androidBooks);
spin.setAdapter(adapter);
稳稳的幸福 2024-12-04 13:33:38

试试这个

 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);

查看此链接

Try this one

 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文