【问题标题】:Is length function of array working in statement of for loop?数组的长度函数是否在 for 循环的语句中工作?
【发布时间】:2015-04-16 14:23:58
【问题描述】:

我正在检查用户输入以控制空白输入。像这样在全局类上声明字符串数组,

public static String[] sortAnswer;

但应用程序在 for 循环行开始时在此代码上爆炸。它已在 for 语句上停止。
for(int i=0; (i < Global.sortAnswer.length );i++) 当我添加像 .length() 这样带有红色标记的括号时。我确定应该是.length 我主要有两个问题 1-) 初始化字符串数组是错误的? 2-) Global.sortAnswer.length 无法计算数组长度?

btnQuestionSortSend.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    edSortQuestion.setText(Global.firstLetterUpper(edSortQuestion.getText().toString()));
    boolean sortCntrol=false;
    for(int i=0; (i < Global.sortAnswer.length );i++)
    {
        if (Global.sortAnswer[i].toString().equals("")||Global.sortAnswer[i].toString()==""||Global.sortAnswer[i].toString().equalsIgnoreCase(""))
        {
            sortCntrol=true;
        }
    }


    if(edSortQuestion.getText().toString().equals(""))
    {
        showMessage(con, getResources().getString(R.string.QuestionSort_fillQuestion), -140,"yellow");
    }
    else if(sortCntrol)
    {
        showMessage(con, getResources().getString(R.string.Question_Empty), -140,"yellow");
    }

谢谢你, 真诚的

【问题讨论】:

  • but application explodes 不能很好地描述问题。请提供尽可能多的有关问题所在的详细信息,特别是如果您没有共享错误/异常/堆栈跟踪。另外,您确定应该length 而不是length()?这是一种方法,而不是属性。
  • 请提供您的异常和堆栈跟踪
  • 您的问题在哪里?
  • 它已停止在 for 语句上。 for(int i=0; (i
  • 当我添加像 .length() 这样带有红色标记的括号时。我确定应该是.length

标签: java android arrays for-loop


【解决方案1】:

很难知道我们是否不知道它抛出的异常。

首先建议检查是否:

  • sortAnswer 是 null
  • sortAnswers 中的一项或多项是null

【讨论】:

  • 不.length() 计算空字符串数组的长度?
  • No @OrGor 它没有,在 null 数组上调用 .length 会抛出 NullPointerException。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 2014-02-09
  • 2019-01-26
  • 1970-01-01
相关资源
最近更新 更多