错误解决:android.view.InflateException: Binary XML file line #11: Error inflating class com.tony.timepicker.TimePicker
今天在做项目开发时遇到这么一个错误,完整的错误提示信息如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tony.timepicker/com.tony.timepicker.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.tony.timepicker.TimePicker
com.tony.timepicker.TimePicker类为一个自定义View类,内容如下:
package com.tony.timepicker; import java.util.Calendar; import android.content.Context; import android.text.format.DateFormat; import android.util.AttributeSet; import android.widget.FrameLayout; import android.widget.NumberPicker; import android.widget.NumberPicker.OnValueChangeListener; public class TimePicker extends FrameLayout { private NumberPicker mHourSpinner; private NumberPicker mMinuteSpinner; private Calendar mDate; private int mHour, mMinute; private String[] mDateDisplayValues = new String[7]; private OnTimeChangedListener mOnTimeChangedListener; public TimePicker(Context context) { super(context); init(context); } public void init(Context context) { mDate = Calendar.getInstance(); // android.widget.TimePicker mHour = mDate.get(Calendar.HOUR_OF_DAY); mMinute = mDate.get(Calendar.MINUTE); inflate(context, R.layout.time_picker, this); updateTimeControl(); mHourSpinner = (NumberPicker) this.findViewById(R.id.np_hour); mHourSpinner.setMaxValue(23); mHourSpinner.setMinValue(0); mHourSpinner.setValue(mHour); mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); mMinuteSpinner = (NumberPicker) this.findViewById(R.id.np_minute); mMinuteSpinner.setMaxValue(59); mMinuteSpinner.setMinValue(0); mMinuteSpinner.setValue(mMinute); mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); } private NumberPicker.OnValueChangeListener mOnDateChangedListener = new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mDate.add(Calendar.DAY_OF_MONTH, newVal - oldVal); updateTimeControl(); onTimeChanged(); } }; private NumberPicker.OnValueChangeListener mOnHourChangedListener = new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mHour = mHourSpinner.getValue(); onTimeChanged(); } }; private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mMinute = mMinuteSpinner.getValue(); onTimeChanged(); } }; private void updateTimeControl() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, -7 / 2 - 1); for (int i = 0; i < 7; ++i) { cal.add(Calendar.DAY_OF_YEAR, 1); mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal); } } public interface OnTimeChangedListener { void onTimeChanged(TimePicker view, int year, int month, int day, int hour, int minute); } public void setOnTimeChangedListener(OnTimeChangedListener callback) { mOnTimeChangedListener = callback; } private void onTimeChanged() { if (mOnTimeChangedListener != null) { mOnTimeChangedListener.onTimeChanged(this, mDate.get(Calendar.YEAR), mDate.get(Calendar.MONTH), mDate.get(Calendar.DAY_OF_MONTH), mHour, mMinute); } } }
经过在网上查找相关问题,找到问题的原因:
因为com.tony.timepicker.TimePicker类没有定义另外两个构造函数TimePicker(Context context, AttributeSet attrs)和TimePicker(Context context, AttributeSet attrs, int defStyle)导致的错误。
解决办法:
定义构造函数TimePicker(Context context, AttributeSet attrs)和TimePicker(Context context, AttributeSet attrs, int defStyle)。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。