0

我的自定义ViewGroup需要一定数量的字符串用于array指定的arrays.xml.

如果不满足该条件,我希望应用程序停止。

对此的最佳做法是什么?

IllegalStateException我应该从 中抛出一个ViewGroup constructor吗?

public MyViewGroup( Context context, AttributeSet attrs )
{
    super( context, attrs );
    if( getResources().getStringArray( R.array.carousellabels ).length != 7 )
        throw new IllegalStateException( "There must exactly 7 items for 
             array resource R.array.carousellabels" );
}
4

1 回答 1

0

抛出和处理异常是昂贵的操作。您可以创建一个工厂方法,以太返回有效实例(如果您获得正确数量的条目)或 null。然后你只需检查 null 并做任何你需要处理的条件(pop Toast?)

于 2009-10-08T04:32:32.633 回答