Spinner听众不工作

时间:2012-06-27 13:59:20

标签: android

我的应用程序崩溃了这段代码..它甚至没有启动..任何想法家伙谢谢 我的应用程序崩溃了这个代码..它甚至没有启动..任何想法家伙谢谢

package com.about.bysk;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;

public abstract class AboutActivity extends Activity implements
        OnItemSelectedListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner spinner = (Spinner) findViewById(R.id.spin);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                Toast.makeText(null, "a", 5);

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
}

这会让我的应用崩溃。请帮忙..

3 个答案:

答案 0 :(得分:1)

您必须为您的微调器设置Listener,您的班级必须实施OnItemSelectedListener

public class YourClass extends Activity implements OnItemSelectedListener { ... }

然后你必须为你的微调器设置Listener

spinner.setOnItemSelectedListener(this);

或者您可以像匿名类一样使用它

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { ... }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) { ... }
});

注意:如果您想要显示Toast,则必须调用show()方法。

答案 1 :(得分:0)

您尚未将lisnter设置为微调器

按照您的代码执行以下操作...

1 - 公共类AboutActivity扩展了活动implement OnItemSelectedListener {

2- spinner。setOnItemSelectedListener(this);

3- Toast.makeText(AboutActivity.this,“RootBox”,Toast.LENGTH_LONG)。show();

您不能将null作为上下文传递给Toast

Toast.makeText(AboutActivity.this, "a", Toast.LENGTH_LONG).show();

答案 2 :(得分:0)

你忘记了setlistner ...你还要用toast打电话给show方法!!