1

我尝试将项目从 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;
    }
4

1 回答 1

5

与活动不同,片段不是上下文的子类,因此您不能this在需要上下文的地方使用。只需使用即可getActivity()

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getActivity(),
        android.R.layout.simple_spinner_item, 
        items_next);
于 2013-03-24T16:16:07.353 回答