android app error java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1

时间:2015-06-16 20:58:30

标签: java android

您好我正在尝试修改学校日程安排应用程序github.com/LiquidPL/Kochanowski以显示从其他网站获取的日程安排。我将MasterlistDownloadRunnable.java中的url修改为“http://gagla.h2g.pl/masterlist”,我将url放入我的日程http://liceumhs-wrze...lan/lista.html。当我运行应用程序并尝试从我的网站同步并下载html计划时,错误会显示在此行中:

shortName = new String (values[1].toCharArray (), 0, values[1].length () - 1);

错误:

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at
com.github.LiquidPL.kochanowski.parse.handler.VulcanHandler.characters(VulcanHandler.java:192)

我还应该在代码中修改哪些内容才能从我的网站下载schdule?

这是VulcanHandler.java的代码

public class VulcanHandler
        extends DefaultHandler
{
    private TimeTableDownloadRunnable runnable;
    private int tableType;

    private String shortName;
    private String longName;

    private int currentLesson = -4;// currentLesson and currentDay are set to negative values
    private int currentDay = -3;   // because of the <tr> and <td> tags at the beginning of
                                   // the HTML file not containing any important data (so the indexing begins from 0)
    private String currentName = "";
    private String currentAttribute = "";
    private int currentGroup = 0; // 0 - current lesson is not grouped; -1 - current lesson is going to be grouped
                                  // 1 - group 1; 2 - group 2
    private String currentSubject = "";
    private String currentTeacher = "";
    private String currentClassroom = "";
    private String currentClass = "";

    private List<String> startTimes = new ArrayList<> ();
    private List<String> endTimes = new ArrayList<> ();

    /**
     * Class constructor
     *
     */
    public VulcanHandler (TimeTableDownloadRunnable runnable, int tableType)
    {
        this.runnable = runnable;
        this.tableType = tableType;
    }

    @Override
    public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
        currentName = qName; // storing the name for use in other methods
        if ("tr".equals (qName)) // increasing the lesson number as we traverse the timetable
        {
            currentLesson++;
            currentDay = -3;
        }
        if ("td".equals (qName) || "th".equals (qName)) currentDay++; // same with the day
        if ("span".equals (qName) && currentGroup == -1) // set the appropriate group number when in group mode
        {
            currentGroup = 1;
        }
        else if ("span".equals (qName) && currentGroup == 1) // same as above
        {
            currentGroup = 2;
        }
        if ("span".equals (qName) || "a".equals (qName)) // if we hit elements that may contain timetable data,
        {                                                // we check if this is the case and store the information
            int length = attributes.getLength ();        // on what data may it be, so we can check it in
            for (int i = 0; i < length; i++)             // characters () method
            {
                String value = attributes.getValue (i);
                // p - subject, n - teacher, s - classroom, o - class
                // tytulnapis is a special case, it contains title of the timetable
                if ("p".equals (value) || "n".equals (value) || "s".equals (value) || "o".equals (value) || "tytulnapis".equals (value))
                {
                    currentAttribute = value;
                }
                if ("font-size:85%".equals (value)) // entering group mode, there will be two lessons within one hour here
                {
                    currentGroup = -1;
                }
            }
        }

        // inserting lesson for the first group, they are separated by a <br/> tag
        if ("br".equals (qName) && currentGroup == 1 && currentDay >= 0 && currentDay <= 4 &&
                !checkIfEmpty (currentSubject, currentTeacher, currentClassroom, currentClass))
        {
            DbWriter.insertLesson (
                    currentDay,
                    startTimes.get (currentLesson),
                    endTimes.get (currentLesson),
                    currentLesson,
                    currentGroup,
                    currentSubject,
                    currentTeacher,
                    currentClassroom,
                    shortName);
            currentSubject = ""; currentTeacher = ""; currentClassroom = ""; currentClass = "";
        }
    }

    @Override
    public void endElement (String uri, String localName, String qName) throws SAXException
    {
        // inserting lesson for the second group, or if the lesson is not grouped
        if ("td".equals (qName) && currentDay >= 0 && currentDay <= 4 &&
            !checkIfEmpty (currentSubject, currentTeacher, currentClassroom, currentClass))
        {
            if (currentGroup == 1 || currentGroup == 2)
            {
                DbWriter.insertLesson (
                        currentDay,
                        startTimes.get (currentLesson),
                        endTimes.get (currentLesson),
                        currentLesson,
                        currentGroup,
                        currentSubject,
                        currentTeacher,
                        currentClassroom,
                        shortName);
            }
            else
            {
                DbWriter.insertLesson (
                        currentDay,
                        startTimes.get (currentLesson),
                        endTimes.get (currentLesson),
                        currentLesson,
                        0,
                        currentSubject,
                        currentTeacher,
                        currentClassroom,
                        shortName);
            }
            currentGroup = 0;
            currentSubject = ""; currentTeacher = ""; currentClassroom = ""; currentClass = "";
        }
    }

    @Override
    public void characters (char[] ch, int start, int length) throws SAXException
    {
        String value = new String (ch, start, length).trim ();
        if (value.length () == 0) return;
        // next for ifs: storing timetable data for later insertion depending on the attribute
        if ("p".equals (currentAttribute))
        {
            currentSubject = value;
        }
        if ("n".equals (currentAttribute))
        {
            currentTeacher = value;
        }
        if ("s".equals (currentAttribute))
        {
            currentClassroom = value;
        }
        if ("o".equals (currentAttribute))
        {
            currentClass = value;
        }
        if ("td".equals (currentName) && currentDay == -1) // storing the lessons begin and end hours
        {
            String[] values = new String (value.toCharArray (), 3, value.length () - 3).split ("-");

            // time must comply with the ISO 8601 standard, so we add a leading zero
            // to the time string if it's only 4 characters in length (eg. 8:55 goes into 08:55)
            if (values[0].length () == 4) values[0] = '0' + values[0];
            if (values[1].length () == 4) values[1] = '0' + values[1];

            startTimes.add (values[0]);
            endTimes.add (values[1]);
        }
        if ("span".equals (currentName) && "-------".equals (value)) // this means that only group 2 has a lesson at this hour
        {
            currentGroup = 2;
        }
        // storing the timetable name, works different for class/teacher and classroom
        if ("span".equals (currentName) && "tytulnapis".equals (currentAttribute))
        {
            String values[] = value.split (" \\(");

            longName = values[0];
         [b]   shortName = new String (values[1].toCharArray (), 0, values[1].length () - 1);[/b]

            if (tableType == Type.CLASS)
            {
                DbWriter.insertClass (shortName, longName);
                runnable.setTableName (longName + " (" + shortName + ")");
            }

            currentAttribute = "";
        }
    }

    /**
     * Checks if the entire set of lesson data have been already collected.
     * @param name Subject name
     * @param code Teacher code
     * @param room Classroom
     * @param _class Class
     * @return
     */
    private Boolean checkIfEmpty (String name, String code, String room, String _class)
    {
        if (name.length () != 0 && code.length () != 0 && room.length () != 0) return false;
        else if (_class.length () != 0 && name.length () != 0 && room.length () != 0) return false;
        else return !(code.length () != 0 && _class.length () != 0 && name.length () != 0);
    }
}

完整错误日志:

06-16 22:35:39.296  32122-32157/com.github.LiquidPL.kochanowski D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-16 22:35:39.302  32122-32122/com.github.LiquidPL.kochanowski D/Atlas﹕ Validating map...
06-16 22:35:39.349  32122-32157/com.github.LiquidPL.kochanowski I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/14/15, ab0075f, Id3510ff6dc
06-16 22:35:39.350  32122-32157/com.github.LiquidPL.kochanowski I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-16 22:35:39.371  32122-32157/com.github.LiquidPL.kochanowski D/OpenGLRenderer﹕ Enabling debug mode 0
06-16 22:35:41.078  32122-32157/com.github.LiquidPL.kochanowski D/OpenGLRenderer﹕ endAllStagingAnimators on 0xb4b6aa80 (RippleDrawable) with handle 0xaec2ce40
06-16 22:35:41.600  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o1.html
06-16 22:35:41.600  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o2.html
06-16 22:35:41.600  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o3.html
06-16 22:35:41.600  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o4.html
06-16 22:35:41.600  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o5.html
06-16 22:35:41.600  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o6.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o7.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o8.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o9.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o10.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o11.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o12.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o13.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o14.html
06-16 22:35:41.601  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o15.html
06-16 22:35:41.602  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o16.html
06-16 22:35:41.602  32122-32180/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o17.html
06-16 22:35:41.945  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o1.html
06-16 22:35:41.945  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o2.html
06-16 22:35:41.946  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o3.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o4.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o5.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o6.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o7.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o8.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o9.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o10.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o11.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o12.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o13.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o14.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o15.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o16.html
06-16 22:35:41.947  32122-32122/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o17.html
06-16 22:35:42.335  32122-32189/com.github.LiquidPL.kochanowski I/Process﹕ Sending signal. PID: 32122 SIG: 9
06-16 22:35:53.899  32209-32229/com.github.LiquidPL.kochanowski D/OpenGLRenderer﹕ endAllStagingAnimators on 0xb4b6cc00 (RippleDrawable) with handle 0xaed6c2f0
06-16 22:35:54.045  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o1.html
06-16 22:35:54.045  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o2.html
06-16 22:35:54.045  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o3.html
06-16 22:35:54.045  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o4.html
06-16 22:35:54.045  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o5.html
06-16 22:35:54.046  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o6.html
06-16 22:35:54.046  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o7.html
06-16 22:35:54.046  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o8.html
06-16 22:35:54.049  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o9.html
06-16 22:35:54.049  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o10.html
06-16 22:35:54.049  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o11.html
06-16 22:35:54.049  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o12.html
06-16 22:35:54.049  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o13.html
06-16 22:35:54.050  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o14.html
06-16 22:35:54.050  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o15.html
06-16 22:35:54.050  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o16.html
06-16 22:35:54.050  32209-32296/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o17.html
06-16 22:35:54.400  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o1.html
06-16 22:35:54.402  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o2.html
06-16 22:35:54.402  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o3.html
06-16 22:35:54.402  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o4.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o5.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o6.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o7.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o8.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o9.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o10.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o11.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o12.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o13.html
06-16 22:35:54.403  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o14.html
06-16 22:35:54.404  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o15.html
06-16 22:35:54.404  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o16.html
06-16 22:35:54.404  32209-32209/com.github.LiquidPL.kochanowski I/liquid﹕ http://liceumhs-wrzesnia.pl/plan/plany/o17.html
06-16 22:35:54.827  32209-32303/com.github.LiquidPL.kochanowski E/AndroidRuntime﹕ FATAL EXCEPTION: pool-1-thread-3
    Process: com.github.LiquidPL.kochanowski, PID: 32209
    java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
            at com.github.LiquidPL.kochanowski.parse.handler.VulcanHandler.characters(VulcanHandler.java:192)
            at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:163)
            at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
            at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
            at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
            at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
            at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
            at javax.xml.parsers.SAXParser.parse(SAXParser.java:390)
            at javax.xml.parsers.SAXParser.parse(SAXParser.java:187)
            at com.github.LiquidPL.kochanowski.parse.TimeTableDownloadRunnable.run(TimeTableDownloadRunnable.java:87)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

在您想要输入特殊索引之前,您应该检查您的阵列是否足够大:

 if(values.length >=2) {
     shortName = new String (values[1].toCharArray (), 0, values[1].length () - 1);
 } else {
     //Your array is to short to make this operation
 }

您可以确定,如果shortName足够大,您只需初始化values