Android InflateException自定义视图
我不明白为什么我的复合视图不起作用Android InflateException自定义视图,android,android-custom-view,layout-inflater,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 Str
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);
}
}
错误主要是由以下原因引起的::android.view.InflateException:二进制XML文件行#2:错误膨胀类
,据我所知,它指的是这一行:膨胀(getContext(),R.layout.component_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>
...
向我们展示您的代码using@Rahul你可以告诉我们你的代码using@Rahul你可以看看