您的位置:首页 > 移动开发 > Android开发

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

2014-07-16 17:31 435 查看
为了方便动态使用自定义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();
}
}


然后报错消失, 执行正常.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息