错误解决:android.view.InflateException: Binary XML file line #11: Error inflating class com.tony.timepic...

今天在做项目开发时遇到这么一个错误,完整的错误提示信息如下:

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)。

 

 

  

转载于:https://www.cnblogs.com/TonyChan7/p/3862594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值