简体   繁体   English

Android:使用if语句循环数组

[英]Android: looping arrays with an if statement

I'm new to Android, but not to programming, and I have to create a prototype app for a Masters project. 我是Android的新手,但不是编程人员,我必须为Masters项目创建一个原型应用程序。 I have 2 predetermined arrays, each containing 33 values. 我有2个预定的数组,每个数组包含33个值。 The aim is to get the arrays displaying each value on a button click. 目的是使数组在单击按钮时显示每个值。 Eventually it'll do more, but that's not important for now. 最终它将做得更多,但是现在这并不重要。

I've used a simple if/else if statement, and enclosed it in a try/finally block. 我使用了一个简单的if / else if语句,并将其包含在try / finally块中。 The arrays loop beautifully, but when the value of i reaches the end of the array (32) and increments past it (33), it doesn't bypass the first if argument and go to the else if block. 数组循环优美,但是当i的值到达数组的末尾(32)并递增超过数组的末尾(33)时,它不会绕过第一个if参数并转到else if块。 Try and try as I might, I cannot for the life of me figure out why it's not returning a false and skipping to the next section on the subsequent iteration. 尽我所能尝试,我一生都无法弄清为什么它没有返回false并跳到后续迭代的下一部分。 The logcat shows it's exceeded the array length, which is fine, but I have an argument in place to catch that. logcat显示它超出了数组长度,这很好,但是我有一个参数可以捕捉到。 Or I think I do? 还是我认为我愿意?

I've probably missed something glaringly stupid, but if anyone out there can spot it, I'd really appreciate the help. 我可能错过了一些愚蠢的东西,但是如果有人可以发现它,我将非常感谢您的帮助。 Thanks in advance :) 提前致谢 :)

Code: 码:

public class TestActivity extends Activity {

public int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    final TextView testQuest = (TextView) findViewById(R.id.questView);
    final TextView testNum = (TextView) findViewById(R.id.numbs);
    Button getNext = (Button) findViewById(R.id.btnNext);

    final String[] quesNums = { "Question 1", "Question 2", "Question 3",
            "Question 4", "Question 5", "Question 6", "Question 7",
            "Question 8", "Question 9", "Question 10", "Question 11",
            "Question 12", "Question 13", "Question 14", "Question 15",
            "Question 16", "Question 17", "Question 18", "Question 19",
            "Question 20", "Question 21", "Question 22", "Question 23",
            "Question 24", "Question 25", "Question 26", "Question 27",
            "Question 28", "Question 29", "Question 30", "Question 31",
            "Question 32", "Question 33" };

    final String[] questions = {
            "I have enough information about the organisation/work area to prepare for my placement.",
            "I am clear about what I need to be able to do to be successful in this role.",
            "I am clear about the skills and qualities I can offer this organisation/work area.",
            "I have a realistic plan to manage financially.",
            "I have a realistic plan to travel to and from the organisation/work area.",
            "I have set realistic goals for myself to prepare for this experience.",
            "I have realistic plan to manage my personal commitments.",
            "My commitments to caring for others (E.g. children, parents) will not be affected by this placement.",
            "Managing my personal needs (E.g. relating to my health, personal experiences, impairment or disability) so they do not have a negative effect on this placement",
            "Overall, I am confident about my preparation for this experience.",
            "Working on specific days/dates that are important to you e.g. Christmas Day, Eid, Diwali.",
            "Being available for the working patterns of your placement.",
            "Being flexible about your personal commitments or needs",
            "Taking regular breaks during the day for your personal commitments or needs.",
            "Feeling comfortable about discussing your personal commitments or needs with your supervisor on the placement.",
            "Using negotiating skills with confidence on your placement.",
            "Assessing the feasibility of asking for time off/alternative working arrangements on your placement. As opposed to during a permanent work contract.",
            "Discussing any problems around managing this placement successfully with your academic supervisor.",
            "Getting to your placement.",
            "Getting to your placement on time.",
            "Getting around the location.",
            "Keeping up with the expected pace of work.",
            "Keeping up with paperwork.", "Achieving your outcomes/goals.",
            "Coping with deadlines.", "Coping with stress.",
            "Coping with personal emergencies.",
            "Working with others or in a team.",
            "Working with people you don't know.",
            "Working with people who are different from you.",
            "Following instructions.",
            "Using your initiative/ work independently.",
            "Managing the hours and schedule of work." };

    i = 0;

    getNext.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {

                if (i <= quesNums.length) {
                    testNum.setText(quesNums[i]);
                    testQuest.setText(questions[i]);
                    i = i + 1;
                }
                else if (i > quesNums.length){
                    testNum.setText("End of questions");
                    testQuest.setText("All done");
                    }
            } finally {
                if (i > 33){
                testNum.setText(i);
                testQuest.setText(i);
                }
            }
        }

    });
}   

}

Logcat: Logcat:

04-14 03:05:33.250: E/AndroidRuntime(1567): java.lang.ArrayIndexOutOfBoundsException: length=33; index=33
04-14 03:05:33.250: E/AndroidRuntime(1567):     at hhs.huddersfield.ppa.TestActivity$1.onClick(TestActivity.java:76)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at android.view.View.performClick(View.java:4091)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at android.view.View$PerformClick.run(View.java:17036)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at android.os.Handler.handleCallback(Handler.java:615)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at android.os.Looper.loop(Looper.java:137)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at android.app.ActivityThread.main(ActivityThread.java:5031)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at java.lang.reflect.Method.invoke(Method.java:511)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-14 03:05:33.250: E/AndroidRuntime(1567):     at dalvik.system.NativeStart.main(Native Method)
04-14 03:05:35.450: E/Trace(1814): error opening trace file: No such file or directory (2)
if (i <= quesNums.length) {
                testNum.setText(quesNums[i]);
                testQuest.setText(questions[i]);
                i = i + 1;
            }

TO

if (i < quesNums.length) {
                testNum.setText(quesNums[i]);
                testQuest.setText(questions[i]);
                i = i + 1;
            }

index in quesNums[index] should be in the range[0,length-1] quesNums [index]中的索引应在[0,length-1]范围内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM