Inflating custom view causes error-Collection of common programming errors
I have created a custom view that includes an ImageButton and a TextView. This is my layout:
I also have a class that inherits from RelativeLayout called CustomImageButton. This is part of it’s code:
public class CustomImageButton extends RelativeLayout {
...
public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomImageButton);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_image_button, this);
this.setClickable(false);
setupViewItems();
}
private void setupViewItems() {
// DO STUFF HERE TO THE IMAGE BUTTON AND TEXT VIEW
// FROM THE CUSTOM ATTRIBUTES
}
}
These are the custom attribute given to the Custom image button: (attrs.xml)
and finally this is the main layout:
and now, for the problem. When i run my application on android 4.0.2 and higher everything is ok BUT, when i run my app on android 2.2.1 i get
android.view.InflateException: Binary XML file line #5: Error inflating class
according to the stack trace (which is VERY uninformative) the exception is thrown from the CustomImageButton
class in the onFinishInflate
method when i call the inflate
method. Any ideas why and how can i fix this?