【问题标题】:if statement not executed in for loop androidif语句未在for循环android中执行
【发布时间】:2012-04-24 05:45:18
【问题描述】:

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

第一条规则的名称是年龄,另一条规则的名称是意外。我已经在此处包含了该方法。

我的日志正在回馈:

规则名称:“年龄”和变量:“事故”并且为真?:“假”

rulename: 'accidents' and variable: 'accidents' and is true ?: 'true'

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());

然后它突然起作用了。

【问题讨论】:

  • 确实看起来应该执行条件块。所以我们可以 100% 确定哪个正在运行,可能会在每个 return 之前抛出一个日志语句(你有没有在调试器中单步执行?)。
  • 您的代码没有任何问题。只需正确调试您的代码,以便检查您的日志语句执行情况。或者可能不同步..!
  • @MichaelBurr 是的,我一步一步地通过调试器。这真是奇怪的行为..
  • 请显示调用代码,并在if语句添加日志记录。
  • @VinceV.:这并没有显示您如何在调用代码中使用结果。我相信该方法正在被调用 - 但我也很容易相信您错误地使用了结果。

标签: java android android-2.2-froyo


【解决方案1】:

你可以试试这个吗?

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

【讨论】:

    【解决方案2】:

    我变了:

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

    到:

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

    然后它似乎起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多