错误解决: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)。

  

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

时间: 2024-11-14 00:29:06

错误解决:android.view.InflateException: Binary XML file line #11: Error inflating class com.tony.timepicker.TimePicker的相关文章

Android项目部署时,发生AndroidRuntime:android.view.InflateException: Binary XML file line #168: Error inflating class错误

这个错误也是让我纠结了一天,当时写的项目在安卓虚拟机上运行都很正常,于是当我部署到安卓手机上时,点击登陆按钮跳转到用户主界面的时候直接结束运行返回登陆界面.    当时,我仔细检查了一下自己的代码,并没有发现什么问题,在logcat上显示的报错如下:AndroidRuntime:android.view.InflateException: Binary XML file line #168: Error inflating class(这是其中报错的最主要的一行信息).  于是我在百度上几乎查看

widget自定义控件【android.view.InflateException: Binary XML file line #2: Error inflating class...】

此错误比较难定位,场景是这样的:在一个widget中使用了自定义控件,始终会报 android.view.InflateException: Binary XML file line #2: Error inflating class...这个错误. 最后实在没有办法了,写了一个demo中只有一个重写的textview(仅仅是继承textview并无其他代码),将其引入到布局文件仍然报同样的错误. 后来经过查找资料,发现removeview比支持复杂的view,连listview都不支持,更不用

android 细节之android.view.InflateException: Binary XML file line #95: Error inflating class(out of m)

今天的异常很有意思,叫做android.view.InflateException: Binary XML file line #95: Error inflating class(out of memory) . 其实是因为out of memory,导致 xml是不可能被充气成功,因此activity的onCreate方法中, setContentView(R.layout.***)也就不可能成功调用. 他出现在我有多个教学动画,并且播放的动画,是基于imageView,imageView的

Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.TabLayout,TableLayout引起页面崩溃

在使用TableLayout的时候,运行引用程序直接Crash. FATAL EXCEPTION: main Process: com.edaixi.activity, PID: 9703 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edaixi.activity/com.edaixi.activity.PriceCenterActivity}: android.view.InflateExcept

bug_ _ android.view.InflateException: Binary XML file line #2: Error inflating class &lt;unknown

===== 2 ==== 1  android.view.InflateException: Binary XML file line #2: Error inflating class <unknown 莫名其妙的出现这个错,看了logcat 说是什么二进制xml错误啥的... 不过后来发现,这个一般都是给eclipse项目中添加图片等资源时出现的一些bug,看似添加到项目中,其实没有,或者是你是选取的引用模式,而你在外面又把那个图片给重命名了,所以在jvm运行时会报错, 说什么xml lin

android.view.InflateException: Binary XML file line #8: Error inflating class &lt;unknown&gt;

错误打印部分代码: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown> 02-02 11:43:11.420: E/AndroidRuntime(25333): at android.view.LayoutInflater.createView(LayoutInflater.java:613) 02-02 11:43:11.420: E/AndroidRuntime(25

android.view.InflateException: Binary XML file line #95: Error inflating class(out of memory) 。异常

android.view.InflateException: Binary XML file line #95: Error inflating class(out of memory) . 其实是因为out of memory,导致 xml是不可能被充气成功,因此activity的onCreate方法中, setContentView(R.layout.***)也就不可能成功调用. 他出现在我有多个教学动画,并且播放的动画,是基于imageView,imageView的背景是我们项目的大型图片

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.LoginActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.LoginActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class 异常解决方案: 是因为设置background的图片太大了,造成了内存溢出,在Activity设置onCreate

android.view.InflateException: Binary XML file line#2: Error inflating class&lt;unknown&gt;

最近在做安卓程序的引导页问题,每次当加载引导页的时候总是报错,说未知的填充错误,很是郁闷啊. 从stackoverflow找到的解决方案: 当运行的时候,安卓会改变图片的大小(基于屏幕的尺寸和分辨率),安卓使用bitmap去重新改变图片的尺寸.所以,这个是内存非常敏感的行为.一个快速的解决方法是,将比较大的图片复制到drawable-ldpi, mdpi, hdpi 文件夹中.安卓会自动选择合适的文件并且重新改变他们的大小, (上面是对stackoverflow的解释) ------------