我试图在用户点击edittext时在我的片段中显示一个DatePickerDialog
//initialise
Calendar newCalendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
etDate.setText(day + "/" + month + "/" + year);
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
//show on click
datePickerDialog.show();
问题是最终:
FATAL EXCEPTION: main
Process: tvfriends.kolio.com.tvproba, PID: 26562
android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.widget.DatePicker
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class android.widget.DatePicker
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:119)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:90)
at tvfriends.kolio.com.tvproba.activities.HostActivity$DatePickerFragment.onCreateDialog(HostActivity.java:311)
at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:307)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.content.res.Resources$NotFoundException: Drawable android:color/tab_indicator_text with resource ID #0x1060009
Caused by: android.content.res.Resources$NotFoundException: File res/color/tab_indicator_text.xml from drawable resource ID #0x1060009
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:725)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.loadDrawable(Resources.java:854)
at android.content.res.TypedArray.getDrawable(TypedArray.java:924)
at android.widget.DatePickerCalendarDelegate.<init>(DatePickerCalendarDelegate.java:155)
at android.widget.DatePicker.createCalendarUIDelegate(DatePicker.java:143)
at android.widget.DatePicker.<init>(DatePicker.java:123)
at android.widget.DatePicker.<init>(DatePicker.java:109)
at android.widget.DatePicker.<init>(DatePicker.java:105)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:119)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:90)
07-28 10:39:55.253 26562-26562/tvfriends.kolio.com.tvproba E/AndroidRuntime: at tvfriends.kolio.com.tvproba.activities.HostActivity$DatePickerFragment.onCreateDialog(HostActivity.java:311)
at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:307)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #18: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:183)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:116)
at android.graphics.drawable.DrawableInflater.inflateFromXml(DrawableInflater.java:130)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1224)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1197)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:715)
... 34 more
我试图将DatePickerDialog设为一个单独的类,但它显示的是类似/相同的错误。
初始化应该触发datepicker的edittext:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.host_activity, container, false);
View footer = inflater.inflate(R.layout.footer_host, container, false);
init(view, footer);
return view;
}
private void init(View view, View footer) {
etDate = (EditText) footer.findViewById(R.id.etDate);
etDate.setOnClickListener(this);
....
}
答案 0 :(得分:2)
我建议您使用第三方库。因为在本地日期选择器中,您在某些API中遇到了一些错误。
compile 'com.wdullaer:materialdatetimepicker:2.4.0'
你可以像下面这样使用它:
Calendar cal = Calendar.getInstance();
int byear = cal.get(Calendar.YEAR);
int bmonth = cal.get(Calendar.MONTH);
int bday = cal.get(Calendar.DAY_OF_MONTH);
com.wdullaer.materialdatetimepicker.date.DatePickerDialog dialog= com.wdullaer.materialdatetimepicker.date.DatePickerDialog.newInstance(new com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(com.wdullaer.materialdatetimepicker.date.DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
etDob.setText(new StringBuilder().append(dayOfMonth)
.append("/").append(monthOfYear + 1).append("/").append(year));
if (etDob.getText().toString().length() == 0) {
etDob.setError("select Date");
} else {
etDob.setError(null);
}
}
}, byear, bmonth, bday);
dialog.setMaxDate(cal);
dialog.show(getActivity().getFragmentManager(),"DatePicker");
如果您不想使用第三方库。然后你可以实现如下。:
private void setOnDateSet() {
date = new DatePickerFragment();
ondate = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int myear, int monthOfYear,
int dayOfMonth) {
etDob.setText(new StringBuilder().append(dayOfMonth)
.append("/").append(monthOfYear + 1).append("/").append(myear));
if (etDob.getText().toString().length() == 0) {
etDob.setError("select Date");
} else {
etDob.setError(null);
}
}
};
}
public class DatePickerFragment extends android.app.DialogFragment {
DatePickerDialog.OnDateSetListener ondateSet;
int byear, bmonth, bday;
public DatePickerFragment() {
}
public void setCallBack(DatePickerDialog.OnDateSetListener ondate) {
ondateSet = ondate;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar cal = Calendar.getInstance();
byear = cal.get(Calendar.YEAR);
bmonth = cal.get(Calendar.MONTH);
bday = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog _date = new DatePickerDialog(getActivity(), ondateSet, byear, bmonth, bday);
_date.getDatePicker().setMaxDate(System.currentTimeMillis());
return _date;
}
}
你可以在edittext click listner上调用它:
setOnDateSet(); // **** Set date on Button text
date.setCallBack(ondate);
date.show(getActivity().getFragmentManager(), "DatePicker");
公开定义:
private DatePickerFragment date;
private DatePickerDialog.OnDateSetListener ondate;