gpt4 book ai didi

android - Tab Fragment错误中创建DatePicker和TimePicker

转载 作者:行者123 更新时间:2023-11-30 01:43:10 25 4
gpt4 key购买 nike

我正在尝试在 tab fragment 中创建 date pickernumber picker,但出现错误。我无法弄清楚错误或者我做错了,因为扩展 fragment 与扩展 AppCompatActivity 不同??

有人可以指出我缺少什么吗?

 public class KeyInWeightF extends Fragment implements View.OnClickListener {

View contentView;
EditText btnTime;
EditText btnDate;
TimePickerDialog timePickerDialog;
DatePickerDialog datePickerDialog;

Calendar calender = Calendar.getInstance();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contentView=inflater.inflate(R.layout.daily_weight_fragement, container, false);
return contentView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

btnTime = (EditText) contentView.findViewById(R.id.time_field);
btnTime.setOnClickListener(this);
btnDate = (EditText) contentView.findViewById(R.id.date_field);
btnDate.setOnClickListener(this);
}

@Override
public void onClick(View v){

switch (v.getId()){

case R.id.time_field:

timePickerDialog = new TimePickerDialog(KeyInWeightF.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

Calendar timeCalendar = Calendar.getInstance();
timeCalendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
timeCalendar.set(Calendar.MINUTE,minute);

String timestring = DateUtils.formatDateTime(KeyInWeightF.this,timeCalendar.getTimeInMillis(),DateUtils.FORMAT_SHOW_TIME);
// KeyInWeightF.this error
btnTime.setText("Time:"+timestring);



}
},calender.get(Calendar.HOUR_OF_DAY),calender.get(Calendar.MINUTE),android.text.format.DateFormat.is24HourFormat(KeyInWeightF.this));
//KeyInWeightF.this error
timePickerDialog.show();
break;

case R.id.date_field:{

datePickerDialog =new DatePickerDialog(KeyInWeightF.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

Calendar dateCalender = Calendar.getInstance();
dateCalender.set(Calendar.YEAR,year);
dateCalender.set(Calendar.MONTH,monthOfYear);
dateCalender.set(Calendar.DAY_OF_MONTH,dayOfMonth);

String dateString = DateUtils.formatDateTime(KeyInWeightF.this,dateCalender.getTimeInMillis(),DateUtils.FORMAT_SHOW_TIME);
// error of KeyInWeightF.this why ?
btnDate.setText("Date:" + dateString);
}
},calender.get(Calendar.YEAR),calender.get(Calendar.MONTH),calender.get(Calendar.DAY_OF_MONTH));

datePickerDialog.show();
break;

}
}
}

错误日志

Error:(65, 54) error: method formatDateTime in class DateUtils cannot be applied to given types;
required: Context,long,int
found: KeyInWeightF,long,int
reason: actual argument KeyInWeightF cannot be converted to Context by method invocation conversion

Error:(72, 114) error: method is24HourFormat in class DateFormat cannot be applied to given types;
required: Context
found: KeyInWeightF
reason: actual argument KeyInWeightF cannot be converted to Context by method invocation conversion

Error:(88, 54) error: method formatDateTime in class DateUtils cannot be applied to given types;
required: Context,long,int
found: KeyInWeightF,long,int
reason: actual argument KeyInWeightF cannot be converted to Context by method invocation conversion

Error:(79, 35) error: no suitable constructor found for DatePickerDialog(KeyInWeightF,<anonymous OnDateSetListener>,int,int,int)
constructor DatePickerDialog.DatePickerDialog(Context,int,OnDateSetListener,int,int,int) is not applicable
(actual and formal argument lists differ in length)
constructor DatePickerDialog.DatePickerDialog(Context,OnDateSetListener,int,int,int) is not applicable
(actual argument KeyInWeightF cannot be converted to Context by method invocation conversion)

我该如何解决?

最佳答案

首先纠正你的onCreateView方法。

        @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contentView=inflater.inflate(R.layout.daily_weight_fragement, container, false);
btnTime = (EditText) contentView.findViewById(R.id.time_field);
btnTime.setOnClickListener(this);
btnDate = (EditText) contentView.findViewById(R.id.date_field);
btnDate.setOnClickListener(this);
return contentView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

Logcat 返回

Error:(79, 35) error: no suitable constructor found for DatePickerDialog(KeyInWeightF,,int,int,int) constructor DatePickerDialog.DatePickerDialog(Context,int,OnDateSetListener,int,int,int) is not applicable (actual and formal argument lists differ in length) constructor DatePickerDialog.DatePickerDialog(Context,OnDateSetListener,int,int,int) is not applicable (actual argument KeyInWeightF cannot be converted to Context by method invocation conversion)

解决方案

您应该使用 getActivity() 而不是 KeyInWeightF.this

getActivity() 在 Fragment 中返回 Fragment 当前关联的 Activity。

timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {

一样
  String timestring = DateUtils.formatDateTime(getActivity(),timeCalendar.getTimeInMillis(),DateUtils.FORMAT_SHOW_TIME);

关于android - Tab Fragment错误中创建DatePicker和TimePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34151809/

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