FATAL EXCEPTION:main java.lang.ClassCastException:添加onItemSelected侦听器时

时间:2014-11-16 19:41:52

标签: java android listener spinner

我正在创建一个基于webservice调用的微调器,其中的项目是循环构建的。

在开始循环之前,我想添加一个默认的“选择项目”项目,但是当我这样做时,我会得到一个致命的例外:

  

致命的例外:主要       java.lang.ClassCastException:com.anyscreeninc.posterviewer.Events无法强制转换为java.lang.CharSequence

if (status.contentEquals("complete")){
    final Spinner event_spinner = (Spinner)findViewById(R.id.events);

    EventAdapter eAdapter = new EventAdapter(posterSessionSetup.this, android.R.layout.simple_spinner_dropdown_item, eventList);

    event_spinner.setAdapter(eAdapter);

//When I add this listener I get the error....
//============================================

    event_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
        Toast.makeText(getApplicationContext(), (CharSequence)  event_spinner.getSelectedItem(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView arg0) {
        Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();        
    }
    });
}

这是我修改过的EventAdapter

public class EventAdapter extends ArrayAdapter<Events> {
    private Activity context;
    ArrayList<Events> data = null;

    public EventAdapter(Activity context, int resource,
                          ArrayList<Events> data) {
        super(context, resource, data);
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return super.getView(position, convertView, parent);
    }


}

我正在根据几个不同的教程构建这些东西。

我试图将Listener中的(CharSequence)更改为(ArrayList),但AndroidStudio告诉我“无法解析方法”

1 个答案:

答案 0 :(得分:0)

这样做应该有用。

Toast.makeText(getApplicationContext(), event_spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();