构造函数ArrayAdapter <string>(LayoutNext,int,String [])未定义</string>

时间:2013-03-24 16:15:10

标签: android

我尝试从android.support.v4.app.Fragment类将项目实现到Spinner。我总是得到编译器错误

  

构造函数ArrayAdapter(LayoutNext,int,String [])未定义

我该如何解决这个问题? 这是代码:

    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 个答案:

答案 0 :(得分:5)

与Activities不同,Fragments不是Context的子类,因此您无法在需要Context的情况下使用this。只需使用getActivity()代替。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getActivity(),
        android.R.layout.simple_spinner_item, 
        items_next);