2016-03-13 82 views
0

我試圖創造一個Android的應用程序的時間表後崩潰,但是當我按下按鈕它崩潰。 我已經嘗試調試它,我發現它總是崩潰,當開關盒結束時,它不關心案件開始的順序。 這是主要課程。 感謝您的幫助!Android應用程序循環

public void setContentView(View view) { 

     //getting information from "database" 
     Subjects subjects1 = new Subjects(9, 10, "test1", "building1", "room1", "teacher1", 5); 
     Subjects subjects2 = new Subjects(8, 11, "test2", "building2", "room2", "teacher1", 1); 

     //creating list and add items 
     List<Subjects> listTimetable = new ArrayList<>(); 
     listTimetable.add(subjects1); 
     listTimetable.add(subjects2); 

     //new TextView with atribs 
     TextView textView = new TextView(this); 
     textView.setTextSize(25); 
     textView.setPadding(0, 0, 60, 0); 


     //get objects for timetable and sett strings 

     for (int i=0; i < listTimetable.size(); i++) { 

      Subjects newOne = listTimetable.get(i); 
      int stringNewOne = newOne.getWeekday(); 

      switch (stringNewOne) { 
       case (1): 
        //setTextView a text 
        textView.setText("Start Time" + newOne.getTimeStart()+"End Time"+newOne.getTimeEnd()+"\n" + " " + 
          newOne.getRoom 
            () + " " + newOne 
          .getBuilding()); 
        TableRow tableRowMon = (TableRow) findViewById(R.id.trMonday); 
        tableRowMon.addView(textView); 

        break; 
       case (2): 
        textView.setText("Start Time" + newOne.getTimeStart()+"End Time"+newOne.getTimeEnd()+"\n" + " " + 
          newOne.getRoom 
            () + " " + newOne 
          .getBuilding()); 
        TableRow tableRowTue = (TableRow) findViewById(R.id.trTuesday); 
        tableRowTue.addView(textView); 
        break; 
       case (3): 
        textView.setText("Start Time" + newOne.getTimeStart()+"End Time"+newOne.getTimeEnd()+"\n" + " " + 
          newOne.getRoom 
            () + " " + newOne 
          .getBuilding()); 
        TableRow tableRowWed = (TableRow) findViewById(R.id.trWednesday); 
        tableRowWed.addView(textView); 
        break; 
       case (4): 
        textView.setText("Start Time" + newOne.getTimeStart()+"End Time"+newOne.getTimeEnd()+"\n" + " " + 
          newOne.getRoom 
            () + " " + newOne 
          .getBuilding()); 
        TableRow tableRowThur = (TableRow) findViewById(R.id.trThrisday); 
        tableRowThur.addView(textView); 
        break; 
       case (5): 
        textView.setText("Start Time" + newOne.getTimeStart()+"End Time"+newOne.getTimeEnd()+"\n" + " " + 
          newOne.getRoom 
            () + " " + newOne 
          .getBuilding()); 
        TableRow tableRowFri = (TableRow) findViewById(R.id.trFriday); 
        tableRowFri.addView(textView); 
        break; 
       case (6): 
        textView.setText("Start Time" + newOne.getTimeStart() + "End Time" + newOne.getTimeEnd() + "\n" + " " + 
          newOne.getRoom 
            () + " " + newOne 
          .getBuilding()); 
        TableRow tableRowSat = (TableRow) findViewById(R.id.trSaturday); 
        tableRowSat.addView(textView); 

        break; 
       case (7): 
        textView.setText("Start Time" + newOne.getTimeStart()+"End Time"+newOne.getTimeEnd()+"\n" + " " + 
          newOne.getRoom 
            () + " " + newOne 
          .getBuilding()); 
        TableRow tableRowSun = (TableRow) findViewById(R.id.trSunday); 
        tableRowSun.addView(textView); 
        break; 

        default: break; 
      } 


     } 







     //add tablerow view Textview 





     //Console debugging Logging 
     Log.d("Test", "message"); 
     String log = Integer.toString(subjects1.getTimeStart()); 
     Log.d("school", log); 


    } 
+0

你可以張貼logcat的???並嘗試打印subject1對象太... –

+0

我們需要一個[MCVE]幫你 –

+0

Xoce웃Пepeúpa 黑帶媒體鏈接給出了正確的答案,但一個快速的問題,我爲什麼要打印的主題嗎? 我這麼問是因爲這樣我就可以在下一次:) @ cricket_007 其最少的代碼,我可以給,沒有這個就woudn't工作,或是否有更好的方法來做到這一點也許幫助我自己,只是想知道下次更好做 – Kira

回答

1

移動

//new TextView with atribs 
    TextView textView = new TextView(this); 
    textView.setTextSize(25); 
    textView.setPadding(0, 0, 60, 0); 

裏面的for循環。一個View不能有多個家長和相同的View不能添加多次