Android应用程序在for循环后崩溃

时间:2016-03-13 16:59:46

标签: java 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);


    }

1 个答案:

答案 0 :(得分:1)

移动

   //new TextView with atribs
   TextView textView = new TextView(this);
   textView.setTextSize(25);
   textView.setPadding(0, 0, 60, 0);
在for循环中

View无法拥有多个父母,同一View无法多次添加