-2

我有一个Logcat error report我无法理解的。你能帮帮我吗?我是 Android 应用程序开发的新手!

07-10 08:51:42.603: E/AndroidRuntime(791): FATAL EXCEPTION: main

07-10 08:51:42.603: E/AndroidRuntime(791): java.lang.ArrayIndexOutOfBoundsException: length=26; index=26

07-10 08:51:42.603: E/AndroidRuntime(791):  at com.example.hangman.Play$1.onClick(Play.java:70)

07-10 08:51:42.603: E/AndroidRuntime(791):  at android.view.View.performClick(View.java:4204)

07-10 08:51:42.603: E/AndroidRuntime(791):  at android.view.View$PerformClick.run(View.java:17355)

07-10 08:51:42.603: E/AndroidRuntime(791):  at android.os.Handler.handleCallback(Handler.java:725)

07-10 08:51:42.603: E/AndroidRuntime(791):  at android.os.Handler.dispatchMessage(Handler.java:92)

07-10 08:51:42.603: E/AndroidRuntime(791):  at android.os.Looper.loop(Looper.java:137)

07-10 08:51:42.603: E/AndroidRuntime(791):  at android.app.ActivityThread.main(ActivityThread.java:5041)

07-10 08:51:42.603: E/AndroidRuntime(791):  at java.lang.reflect.Method.invokeNative(Native Method)

07-10 08:51:42.603: E/AndroidRuntime(791):  at java.lang.reflect.Method.invoke(Method.java:511)

07-10 08:51:42.603: E/AndroidRuntime(791):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

07-10 08:51:42.603: E/AndroidRuntime(791):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

07-10 08:51:42.603: E/AndroidRuntime(791):  at dalvik.system.NativeStart.main(Native Method)

我的 OnClickListener() 代码如下: View.OnClickListener go_func = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        int flag = 0;
        TextView status = (TextView) findViewById(R.id.status);
        TextView chances = (TextView) findViewById(R.id.chances);
        TextView word = (TextView) findViewById (R.id.word);
        EditText guessed = (EditText) findViewById(R.id.letter_guessed);
        String letter = guessed.getText().toString();
        char[] Letter = letter.toCharArray();
        String current_status = status.getText().toString();
        String current_chances = chances.getText().toString();
        String current_word = new String(); 

        //checking if letter is already guessed
        for(int i=0;i<letters_guessed.length;i++)
        {
            if(Letter[0] == letters_guessed[i])
            {
                current_status = "Letter Already Guessed!";
                status.setText(current_status);
            }
            break;
        }

        //if letter is not already guessed
        if(current_status !="Letter Already Guessed!")
        {
            int len=0;
            int i=0;
            while(letters_guessed[i] != '\u0000')
            {
                len++;
                i++;
            }
            if(len !=0)
                letters_guessed[len] = Letter[0];
            else
                letters_guessed[0] = Letter[0];

            for(i=0;i<word_chosen.length();i++)
            {   
                flag = 0;
                for(int j=0;j<letters_guessed.length;j++)
                {

                    if(word_final[i]==letters_guessed[j])
                    {
                        char[] temp_array = current_word.toCharArray();
                        temp_array[temp_array.length] = word_final[i];
                        current_word = temp_array.toString();
                        flag=1;
                        break;
                    }

                }
                if(flag==0)
                {
                    char[] temp_array = current_word.toCharArray();
                    temp_array[temp_array.length] = '_';
                    current_word = temp_array.toString();
                }

            }
        }
        int a=0;
        char[] word_check = word_chosen.toCharArray();
        for(int i=0;i<word_check.length;i++)
        {
            if(word_check[i] == Letter[0])
            {
                a=1;
                status.setText("Correct guess!");
            }
        }
        if(a==0)
        {
            status.setText("Wrong guess!");
        } 
        word.setText(current_word);

    }
};
4

3 回答 3

1

LogCat 显示崩溃发生时应用程序不同类的执行时间:

顶行显示您有FATAL Exception由于下一行ArrayIndexOutOfBoundsException(您正在尝试访问大小为 26 的数组中的第 26 项,因为编号是从 0-25)

onClick()接下来,当您的应用程序在您的内部方法中执行第 70 行时,会导致此异常com.example.hangman.Play Activity

at com.example.hangman.Play$1.onClick(Play.java:70)

剩下的几行是由 Android 系统运行(并且在崩溃发生时正在执行)以在onClick()内部执行的其余类。

总而言之,您ArrayIndexOutOfBoundsException在Activityline 70onClick()方法中拥有Play

于 2013-07-10T12:09:08.060 回答
0

ArrayIndexOutOfBoundsException 告诉您在访问数组时使用了错误的索引。您的数组大小为 26,索引应为 0 到 25。

改变这个

while(letters_guessed[i] != '\u0000')
        {
            len++;
            i++;
        }

while(i < letters_guessed.length - 1 && letters_guessed[i] != '\u0000')
        {
            len++;
            i++;
        }
于 2013-07-10T12:02:06.260 回答
0

为了帮助您将来解决自己的问题(不仅仅是这个问题),了解如何有目的地使用 Android Logger 会很有帮助。这里有一个很好的资源了解如何使用您要记录的不同类型的信息来完成此操作。

如果您一目了然地无法轻易找到问题,我会做的是有目的地使用 Logger 来找出您可能会在哪里得到意想不到的结果。例如:

//checking if letter is already guessed
Log.d("Play", "letters_guessed.length = " + letters_guessed.length);
for(int i=0;i<letters_guessed.length;i++)
{
    Log.d("Play", "i = " + i);
    Log.d("Play", "Letter[0] = " + Letter[0]);
    if(Letter[0] == letters_guessed[i])
    {
        current_status = "Letter Already Guessed!";
        status.setText(current_status);
    }
    break;
}
于 2013-07-10T12:25:17.600 回答