2 sachinks SachinKS 于 2013.03.25 11:37 提问

android中应用编译报错

一个带下拉列表的应用,在编译的时候报错:

The constructor ArrayAdapter(LayoutNext, int, String[]) is undefined

不知道应该怎么解决?

我的实现代码:

   public class LayoutNext extends Fragment 
                            implements OnClickListener,OnItemSelectedListener{

    TimePicker timepicker;
    private Spinner spinner_next;
    public ArrayAdapter<String> adapter;

    public static Fragment newInstance(Context context) {
        LayoutNext f = new LayoutNext();    

        return f;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_next, null);  

        timepicker = (TimePicker) root.findViewById(R.id.timePicker1);
        timepicker.setIs24HourView(true);
        timepicker.setCurrentHour(0);
        timepicker.setCurrentMinute(0);

        String[] items_next = { "Next", "From to"};
        spinner_next = (Spinner) root.findViewById(R.id.sp_next);
        spinner_next.setOnItemSelectedListener(this);

 // Here I get the error
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_spinner_item, 
                items_next);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_next.setAdapter(adapter);

        return root;
    }

1个回答

Kakalapa1986
Kakalapa1986   2013.03.25 14:26
已采纳

Fragment不是context的子类,所以不能使用this。只要换成getActivity()就行。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getActivity(),
        android.R.layout.simple_spinner_item, 
        items_next);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!