zoukankan      html  css  js  c++  java
  • 错误解决: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)。

      

  • 相关阅读:
    自定义predicate来对List进行去重
    ParameterizedType
    万级TPS亿级流水-中台账户系统架构设计
    【Linux】【压测】基于python Locust库实现自动化压测实践
    家里宽带ADSL是城域网IP以及公网IP的具体区别和联系
    fiddler如何使用Xposed+JustTrustMe来突破SSL Pinning
    幽门螺旋杆菌检查方法那么多,到底选择哪个?
    apache (httpd)不支持中文路径问题
    皮肤瘙痒症、干燥瘙痒、荨麻疹和丘疹性荨麻疹区别和联系
    Redmi K30和Redmi K30 5G和Redmi K30 5G极速版和Redmi K30i 5G
  • 原文地址:https://www.cnblogs.com/TonyChan7/p/3862594.html
Copyright © 2011-2022 走看看