在TextView中循环的单词会导致崩溃

时间:2012-09-02 12:48:30

标签: java android eclipse loops textview

问题得到解决,感谢每个人的帮助

我有一个带有单词的String数组,数组是MAXWORDS很长,我有5个TextView框,这些单词应该从一个框到下一个框这个工作正常,我的问题是,当没有数组中的更多单词我希望它从第一个单词重新开始,并从头开始循环遍历单词,这样可以正常工作,直到所有的单词框中都有他们的初始单词,但是在我的程序崩溃之后又多了一步我无法弄清楚为什么

继承人是一些条纹代码

public class SwiftFingersGame extends Activity {

static int MAXWORDS = 6;    //Max words in memory

String[] words;     // contains all words
int wordsUsed;
//hold TextViews
TextView nextWord4;
TextView nextWord3;
TextView nextWord2;
TextView nextWord1;
TextView nextWord;

  @Override
    public void onCreate(Bundle savedInstanceState) {

        //Initiating variables
        words = new String[MAXWORDS];
        wordsUsed = 0;
        words[0] = "one";
        words[1] = "two";
        words[2] = "three";
        words[3] = "four";
        words[4] = "five";
        words[5] = "six";


        //Grabbing text boxes
        nextWord4 = (TextView) findViewById(R.id.word4);
        nextWord3 = (TextView) findViewById(R.id.word3);
        nextWord2 = (TextView) findViewById(R.id.word2);
        nextWord1 = (TextView) findViewById(R.id.word1);
        nextWord = (TextView) findViewById(R.id.word);


    }

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

            if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE)
                { 
                    wordsUsed++;
                    updateWords(wordsUsed);
                    return super.onKeyDown(keyCode, event);
                }
            return super.onKeyDown(keyCode, event);
        }


Here are the code i use to update my TextViews

    //Putting words into text boxes
        public void updateWords(int next){
            nextWord.setText(words[next % MAXWORDS]);
            nextWord1.setText(words[(1+next) % MAXWORDS]);
            nextWord2.setText(words[(2+next) % MAXWORDS]);
            nextWord3.setText(words[(3+next) % MAXWORDS]);
            nextWord4.setText(words[(4+next) % MAXWORDS]);
        }

让我说我的六个单词“one”,“two”......“五”,“六”在我的单词数组中

框中以“一个”开头 - “五个”在我们的代码运行6次时它们再次有“一个” - “五个”但第七次我的代码运行它导致崩溃时的单词成为“两个” - “六个”

当Eclipse中的debuger在每次更新之前暂停时,debuger会打开一个“ViewRootImpl.deliverInputEvent(ViewRootImpl $ QueuedInputEvent)”

未找到来源。

带有“编辑源查找路径...”按钮

我是Java和Android开发的新手,所以如果我的想法中有一些明显错误的事情我很抱歉,但感谢所有帮助

编辑,我相信我可能一直在使用logCat错误,所以这是另一个尝试只有我的程序崩溃时出现的错误:

09-02 13:56:11.772: E/AndroidRuntime(609): FATAL EXCEPTION: main
09-02 13:56:11.772: E/AndroidRuntime(609): java.lang.ArrayIndexOutOfBoundsException: length=6; index=6
09-02 13:56:11.772: E/AndroidRuntime(609):  at com.swiftfingers.swift.SwiftFingersGame.onKeyDown(SwiftFingersGame.java:185)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.app.Activity.dispatchKeyEvent(Activity.java:2361)
09-02 13:56:11.772: E/AndroidRuntime(609):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1819)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3575)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:3531)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3113)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2930)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.os.Looper.loop(Looper.java:137)
09-02 13:56:11.772: E/AndroidRuntime(609):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-02 13:56:11.772: E/AndroidRuntime(609):  at java.lang.reflect.Method.invokeNative(Native Method)
09-02 13:56:11.772: E/AndroidRuntime(609):  at java.lang.reflect.Method.invoke(Method.java:511)
09-02 13:56:11.772: E/AndroidRuntime(609):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-02 13:56:11.772: E/AndroidRuntime(609):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-02 13:56:11.772: E/AndroidRuntime(609):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案