SachinKS 2013-03-25 03:37 采纳率: 0%
浏览 3225
已采纳

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 2013-03-25 06:26
    关注

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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getActivity(),
            android.R.layout.simple_spinner_item, 
            items_next);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容