我想在单击图像按钮时弹出日期选择器对话框,然后用户选择的日期将显示在编辑文本字段中。我按照Android开发人员的指南使用DialogFragment,并成功弹出日期选择器对话框。用户可以在对话框中选择日期,但当用户按下设置按钮时,它将崩溃。
实际上我怀疑这是来自onDateSet中LayoutInflater中的getContext()的问题,因为有一句话说"调用所需的API级别23(当前API:12)",但我无法弄清楚如何解决它。
Android开发人员建议调用getContext()来获取活动的Context,并且不确定问题发生的原因。
我曾尝试使用@TargetApi,但它表示这仅用于该方法。
有人可以告诉我代码中的问题在哪里吗?
逻辑说明: java.lang.NoSuchMethodError:com.test.testapplication.DatePickerFragment.getContext
DatePickerFragment
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
final Calendar calendar = Calendar.getInstance();
int YEAR;
int MONTH;
int DAY;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
YEAR = calendar.get(Calendar.YEAR);
MONTH = calendar.get(Calendar.MONTH);
DAY = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, YEAR, MONTH, DAY);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
YEAR = year;
MONTH = monthOfYear;
DAY = dayOfMonth;
String dateFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.CHINESE);
View fragment = LayoutInflater.from(getContext()).inflate(R.layout.activity_datepickerdialogfragment, null);
EditText editText = (EditText) fragment.findViewById(R.id.edittext);
editText.setText(sdf.format(calendar.getTime()));
}
}
MainActivity
public class DPDFActivity extends FragmentActivity {
EditText editText;
ImageButton btn;
DialogFragment datePickerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepickerdialogfragment);
editText = (EditText) findViewById(R.id.edittext);
btn = (ImageButton) findViewById(R.id.btn);
btn.setOnClickListener(new showDateDialog());
}
public class showDateDialog implements View.OnClickListener {
@Override
public void onClick(View v) {
datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getFragmentManager(), "DatePicker");
}
}
}
答案 0 :(得分:1)
在你的活动中创建日期选择器类作为静态,例如我在我的片段中做了类似的事情。
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
int currentYear;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
currentYear = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, currentYear, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
if(currentYear-year >=15) {
CharSequence strDate = null;
Calendar chosenDate = Calendar.getInstance();
chosenDate.setTimeInMillis(0);
chosenDate.set(year, month, day);
Date date = chosenDate.getTime();
SimpleDateFormat dateFormatter = new SimpleDateFormat(
"yyyy-MM-dd");
strDate = dateFormatter.format(date);
btnDOB.setText(strDate);
}
else
{
Toast.makeText(view.getContext(),"Selected Year should be mininmum 15 year ago from current year. Please try again.",Toast.LENGTH_LONG).show();
}
}
}
在您的活动中定义任何方法(类级别)之外的edittext
private static EditText meditText;
只在活动oncreate()中使用findById()方法,以后可以更新。
答案 1 :(得分:0)
而不是尝试调用getContext()。你应该可以在这里使用getActivity()。
View fragment = LayoutInflater.from(getActivity()).inflate(R.layout.activity_datepickerdialogfragment, null);