2012-04-24 105 views
2

我有一个非常奇怪的问题。我有这个包含两个规则的Procedure对象。现在我想用for循环来循环这些规则并返回1规则。但我总是得到空。看起来他正在跳过if语句并直接进入我的返回null部分。如果语句不执行for循环android

第一条规则的名称是年龄,另一条规则的名称是事故。我在这里包括了这个方法。

我的日志回馈:

RuleName中: '年龄' 和变量: '事故',是真的? '假'

RuleName中: '事故' 和变量: '事故'并且是真实的: '真'

private Rule searchRule(String ruleName) 
    { 
     List<Rule> test = this.procedureObject.getRules(); 

     for(Rule rule : test) 
     { 
      Log.d("debug", "rulename: '" + rule.getName() + "' and variable: '"+ruleName+"' and is true ?: '"+rule.getName().equalsIgnoreCase(ruleName)+"'"); 
      if(rule.getName().equalsIgnoreCase(ruleName)) 
      { 
       return rule; 
      } 
     } 

     return null; 
    } 

编辑:现在的工作。

我只是改变了这种调用代码:

Rule makeRule = RuleFactory.getNewRuleClassNameInstance(this.searchRule(ref2.getName()).getClass().getSimpleName()); 

到:

Rule foundRule = this.searchRule(ref2.getName()); 

Rule makeRule = RuleFactory.getNewRuleClassNameInstance(foundRule.getClass().getSimpleName()); 

然后它突然的作品。

+0

它肯定看起来应该执行条件块。因此,我们可以100%确定哪个正在运行,可能在每个“返回”之前抛出一个日志语句(是否已经在调试器中进行了检查?)。 – 2012-04-24 05:52:26

+0

你的代码没有问题,只要正确调试你的代码,这样你就可以检查你的Log语句执行。或者可能不同步。 – user370305 2012-04-24 05:52:38

+0

@MichaelBurr是的,我一步一步地通过调试器。这真是奇怪的行为.. – 2012-04-24 05:54:55

回答

0

我改变:

Rule makeRule = RuleFactory.getNewRuleClassNameInstance(this.searchRule(ref2.getName()).getClass().getSimpleName()); 

到:

Rule foundRule = this.searchRule(ref2.getName()); 
Rule makeRule = RuleFactory.getNewRuleClassNameInstance(foundRule.getClass().getSimpleName()); 

,然后它似乎工作。

0

你可以试试吗?

if(rule.getName().equalsIgnoreCase(ruleName)) 
{ 
    Log.d("debug", "rulename: '" + rule.getName() + "' and variable: '"+ruleName+"' and is true ?: '"+rule.getName().equalsIgnoreCase(ruleName)+"'"); 
    return rule; 
}