0

私のカスタムでは、指定された inViewGroupに対して特定の数の文字列が必要です。arrayarrays.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 をチェックし、条件を処理するために必要なことを実行します (Toast をポップしますか?)。

于 2009-10-08T04:32:32.633 に答える