2009-10-07 82 views
0

我的自定義ViewGroup需要arrays.xml中指定的array的一定數量的字符串。如果條件不符合,則中止ViewGroup?

我希望應用程序停止,如果不滿足條件。

這是什麼最佳做法?

我應該只是從ViewGroup constructorIllegalStateException

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

回答

0

投擲和處理異常是昂貴的操作。您可以創建一個工廠方法,該方法可以返回有效的實例(如果您獲得了正確的條目數)或null。然後,你只需檢查空,並做任何你需要處理的條件(流行吐司?)

相關問題