【问题标题】:Aborting ViewGroup if a condition is not met?如果不满足条件则中止 ViewGroup?
【发布时间】:2009-10-07 08:32:25
【问题描述】:

我的自定义ViewGroup 期望arrayarrays.xml 中指定一定数量的字符串。

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

这方面的最佳做法是什么?

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

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

【问题讨论】:

    标签: java android mobile android-widget


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      相关资源
      最近更新 更多