Android InflateException自定义视图

时间:2017-01-12 03:40:38

标签: android android-custom-view layout-inflater

不要理解为什么我的复合视图不想工作。

Caused by: android.view.InflateException: Binary XML file line #186: Error inflating class com.igor.customviews.CompoundEditText

有建设者。如你所见,我没有错过super并将init()传递给所有人。

public class CompoundEditText extends RelativeLayout {

private String editTextHint;
private int resDrawableLeft, resDrawableRight, inputType;

private EditTextRegular editText;
private ImageView customDrawableLeft, customDrawableRight;

public CompoundEditText(Context context) {
    super(context);
    init();
}

public CompoundEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.CompoundEditText,
            0, 0);

    try {
        editTextHint = a.getString(R.styleable.CompoundEditText_hint);
        resDrawableLeft = a.getResourceId(R.styleable.CompoundEditText_left_drawable_src, 0);
        resDrawableRight = a.getResourceId(R.styleable.CompoundEditText_right_drawable_src, 0);

        editText.setHint(editTextHint);
        customDrawableLeft.setImageResource(resDrawableLeft);
        customDrawableRight.setImageResource(resDrawableRight);
    } finally {
        a.recycle();
    }
}

public CompoundEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init(){
    inflate(getContext(), R.layout.compound_edit_text, this);

    editText = (EditTextRegular) findViewById(R.id.edit_text);
    customDrawableLeft = (ImageView) findViewById(R.id.drawable_left);
    customDrawableRight = (ImageView) findViewById(R.id.drawable_right);
}
}

错误主要是Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>,据我所知,它引用了这一行:inflate(getContext(), R.layout.compound_edit_text, this);

我无法弄清楚为什么它没有帮助,因为我制作另一个复合视图的方式与它一样正常。

希望您对代码的全新审视有助于解决问题。谢谢。

修改

尝试将其添加到布局中。

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_bg_color"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
...
<com.igor.customviews.CompoundEditText
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />


</LinearLayout>

0 个答案:

没有答案