2014-04-14 2 views
0

Android는 처음이지만 프로그래밍은 할 수 없으므로 Masters 프로젝트의 프로토 타입 응용 프로그램을 만들어야합니다. 각 배열에는 33 개의 값이 들어있는 미리 지정된 배열이 2 개 있습니다. 목표는 버튼 클릭시 각 값을 표시하는 배열을 가져 오는 것입니다. 결국 더 많은 일을 할 것이지만, 지금은 중요하지 않습니다.Android : if 문을 사용하여 반복 배열

간단한 if/else if 문을 사용하고 try/finally 블록으로 묶었습니다. 배열은 아름답게 반복되지만, i의 값이 배열 (32)의 끝에 도달하고 그 값이 증가하면 (33) 첫 번째 if 인수를 우회하지 않고 else if 블록으로 이동합니다. 내가 시도한대로 시도해보고 시도해보십시오. 왜 내가 거짓말을하지 않고 후속 반복의 다음 섹션으로 건너 뛰지 않는지 알아낼 수 없습니다. logcat은 배열 길이를 초과했음을 보여 주지만 괜찮습니다.하지만이를 잡으려고하는 인수가 있습니다. 또는 나는 생각한다?

나는 어쩌면 눈부신 바보 같은 것을 놓쳤을 것입니다. 그러나 누군가가 그것을 발견 할 수 있다면 정말 도움이됩니다.

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

    }); 
} 

} 

로그 캣 : 사전 :

코드 감사 quesNums에서

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) 

답변

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

인덱스

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

TO는 [인덱스] 범위 [에 있어야합니다 0, 길이 -1]

+0

정말 고마워요! 이제 완벽하게 작동합니다! 내가 그걸 놓쳤다는 것을 믿을 수는 없지만, 당신은 나를 매우 행복하게 만들었습니다. 고마워요, 고마워요! – Cazzers

관련 문제