如何重现android.view.inflateException

时间:2014-03-12 09:47:38

标签: android xml runtimeexception inflate-exception

如何在将布局XML文件实例化为相应的View对象时自然调用android.view.InflateException?禁止使用AttributeSet参数和throw擦除构造函数。

使用典型构造函数集:

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();
}

1 个答案:

答案 0 :(得分:0)

由于InflateException继承自RuntimeException,您可以自由地抛出它