我有一个名为RelativeLayout
的{{1}}扩展的类。在它的构造函数中,我正在做:
LabelMenuButton
我在活动的XML布局中使用public LabelMenuButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LabelMenuButton, 0, 0);
String headingText;
String valueText;
try {
headingText = a.getString(R.styleable.LabelMenuButton_headingText);
valueText = a.getString(R.styleable.LabelMenuButton_valueText);
} finally {
a.recycle();
}
addView(inflate(getContext(), R.layout.view_label_menu_button, this));
tvHeading = (TextView) findViewById(R.id.tv_lbl_menu_btn_heading);
tvValue = (TextView) findViewById(R.id.tv_lbl_menu_btn_value);
setHeadingText(headingText);
setValueText(valueText);
}
类:
LabelMenuButton
但是,我相信,当我正在设定活动的内容时,我会得到以下异常:
<com.blah.blah.blah.LabelMenuButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="150dp"
app:headingText="Sample heading"
app:valueText="Sample value" />
view_label_menu_button.xml:
Unable to start activity ComponentInfo{com.blah.blah.blah/com.blah.blah.blah.MyActivity}:
android.view.InflateException: Binary XML file line #105: Error inflating class com.blah.blah.blah.LabelMenuButton
答案 0 :(得分:1)
addView(inflate(getContext(), R.layout.view_label_menu_button, this));
inflate()
已将膨胀的视图附加到提供的父根(此处为this
),除非您使用attachToRoot
且false
设置为addView()
。再次将视图添加到父级会导致“已有父级”异常。
您可以在此处删除{{1}}。