0

How to naturally invoke android.view.InflateException whilst instantiation a layout XML file into a corresponding View object? Erasure of the constructor with AttributeSet parameter and throw is prohibited.

Use set of typical constructors:

public TestView(Context context) {
    this(context, null);
}

public TestView(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.gridViewStyle);
}

public TestView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs,
            com.android.internal.R.styleable.GridView, defStyle, 0);

    int hSpacing = a.getDimensionPixelOffset(
            com.android.internal.R.styleable.GridView_horizontalSpacing, 0);
    setHorizontalSpacing(hSpacing);

    int vSpacing = a.getDimensionPixelOffset(
            com.android.internal.R.styleable.GridView_verticalSpacing, 0);
    setVerticalSpacing(vSpacing); 

    int index = a.getInt(com.android.internal.R.styleable.GridView_stretchMode, STRETCH_COLUMN_WIDTH);
    if (index >= 0) {
        setStretchMode(index);
    }

    int columnWidth = a.getDimensionPixelOffset(com.android.internal.R.styleable.GridView_columnWidth, -1);
    if (columnWidth > 0) {
        setColumnWidth(columnWidth);
    }

    int numColumns = a.getInt(com.android.internal.R.styleable.GridView_numColumns, 1);
    setNumColumns(numColumns);

    index = a.getInt(com.android.internal.R.styleable.GridView_gravity, -1);
    if (index >= 0) {
        setGravity(index);
    }

    a.recycle();
}
Yehor Nemov
  • 907
  • 2
  • 16
  • 31
  • I'm not sure what you're trying to achieve, especially given the constraints. However, any `Exception` coming out from `View` constructor will do, see http://androidxref.com/4.4.2_r1/xref/frameworks/base/core/java/android/view/LayoutInflater.java#594 – laalto Mar 12 '14 at 10:14
  • The purpose of this is to determine an origin of inflation error for `TestView` which extends `AbsListView` while changing the view instance from the class to `GridView` one turns off the issue and runtime become safe. – Yehor Nemov Mar 12 '14 at 13:19

1 Answers1

0

Since InflateException inherits from RuntimeException you can throw it freely

Gaskoin
  • 2,469
  • 13
  • 22