gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:23:35 25 4
gpt4 key购买 nike

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

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;
}

最佳答案

与 Activity 不同,Fragment 不是 Context 的子类,因此您不能在需要 Context 的地方使用 this。只需使用 getActivity() 即可。

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

关于android - 构造函数 ArrayAdapter<String>(LayoutNext, int, String[]) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15600845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com