0

我的应用程序因此代码而崩溃.. 它甚至无法启动.. 任何想法,谢谢我的应用程序因此代码而崩溃.. 它甚至无法启动.. 任何想法,谢谢

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

            }
        });
    }
}

这使我的应用程序崩溃。请帮忙..

4

3 回答 3

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()方法。

于 2012-06-27T14:01:07.140 回答
0

您还没有将 lisnter 设置为 spinner

根据您的代码执行以下操作...

1-公共类 AboutActivity 扩展 Activity implement OnItemSelectedListener{

2-微调器。setOnItemSelectedListener(this);

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

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

Toast.makeText(AboutActivity.this, "a", Toast.LENGTH_LONG).show();
于 2012-06-27T14:01:24.323 回答
0

你忘了 setlistner ......你也用 toast 调用 show 方法!

于 2012-06-27T14:03:52.083 回答