android自定义控件inflate报错view.inflate.exception

来源:互联网 发布:淘宝服装ps教程 编辑:程序博客网 时间:2024/05/20 18:00

为了方便动态使用自定义KeyboardView, 对其进行了封装

public class KeyboardView extends FrameLayout {private Context mcontext;public KeyboardView(Context context) {super(context);mcontext = context;initComponents();}}

但是每次在MainActivity中执行到加载的时候:

keyboardView = (KeyboardView)stub.inflate();


就会报错: view.inflate.exception, 首先发现在xml中自己直接使用了:

<KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="300dp"    android:layout_gravity="bottom" >


 <KeyboardView /> 标签, 而androd有个自带的控件:

android.inputmethodservice.KeyboardView, 所以将xml中标签改成了:

<com.yzh.lockpri2.widget.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="300dp"    android:layout_gravity="bottom" >


但是问题依旧, 后来突然想到在重载FrameLayout的时候, 还有两个构造方法没有覆盖, 然后在代码中加上另外两个方法:

public class KeyboardView extends FrameLayout {private Context mcontext;public KeyboardView(Context context) {super(context);mcontext = context;initComponents();}public KeyboardView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);mcontext = context;initComponents();}public KeyboardView(Context context, AttributeSet attrs) {super(context, attrs);mcontext = context;initComponents();}}


然后报错消失, 执行正常.

0 0
原创粉丝点击