android.view.InflateException: Binary XML file line #1: Error inflating class

来源:互联网 发布:淘宝客服月会总结 编辑:程序博客网 时间:2024/06/09 22:36

很基础的一个问题:自定义的类继承与Scrollview,初始化的时候报错:

android.view.InflateException: Binary XML file line #1: Error inflating class
java代码如下:

自定义类

public class MyScrollview extends ScrollView {    private static final String TAG = "MyScrollview";    public MyScrollview(Context context) {        super(context);    }

调用自定义类

 MyScrollview myScrollview;        myScrollview = (MyScrollview) findViewById(R.id.scrollView3);

布局文件

 <com.example.jiabl.scrollview.MyScrollview        android:id="@+id/scrollView3"        android:layout_width="0dp"        android:layout_height="0dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        android:scrollbars="none">        <TextView            android:id="@+id/textView4"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="@string/news"            android:textSize="24sp" />    </com.example.jiabl.scrollview.MyScrollview>

报错原因:

派生类没有继承父类的 (Context context, AttributeSet attrs)构造版本,xml布局文件调用的就是这个构造函数,添加这个构造函数即可

public MyScrollview(Context context, AttributeSet attrs) {        super(context, attrs);    }

阅读全文
0 0