0

现在我正在编写一个信使程序。以下代码应存储消息历史记录。可悲的是,我在一行上遇到了“IndexOutOfBounds”异常,我已经尝试了一整天,但仍然无法找出问题所在。

所以就在这里。

    public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final String FILE_PATH = "/data/data/com.demo.xmppchat/";
public static Map<Date, String> messagesMap = new TreeMap<Date, String>();

// File should be saved as /username/buddy.xml
public static void writeHistory(String username, String buddy, Map<Date, String> messages)
{
    try {           

        // Check if the file exists. If the file does not exist, create and initiate one. 
        File file = new File(FILE_PATH + username);
        if (!file.exists()) {
            file.mkdirs();
        }   
        file = new File(FILE_PATH + username + "/" + buddy + ".xml");
        // file.setReadable(true); file.setWritable(true); file.setExecutable(true);
        if (!file.exists()) {
            file.createNewFile();               
            initiateHistoryFile(username, buddy);
        }

        // Now that the XML file should exists as we have created and initialized one.

        // TODO:
        // Now we modify the XML file

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(file);
        Element root = doc.getDocumentElement();
        Element rootE = doc.getDocumentElement();   
        String s = "";
        Node n = null;

        for (Map.Entry<Date, String> entry : messages.entrySet()) {     

            // <message>
            Element messageE = doc.createElement("message");
            rootE.appendChild(messageE);

            // Set attribute to <message> element
            // messageE.setAttribute("id", "1");

            // <from>
            Element fromE = doc.createElement("from");
            fromE.appendChild(doc.createTextNode(buddy));
            messageE.appendChild(fromE);

            // <date>
            Element dateE = doc.createElement("date");
            s = SDF.format(entry.getKey());
            n = doc.createTextNode(s);
            dateE.appendChild(n);
            messageE.appendChild(dateE);

            // <text>
            Element textE = doc.createElement("text");
            s = entry.getValue();
            n = doc.createTextNode(s);
            textE.appendChild(n);
            messageE.appendChild(textE);    

            root.appendChild(messageE); // <- *** THIS IS WHERE "IndexOutOfBounds" exception OCCURS! ***
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(file);
        transformer.transform(source, result);

        // Write the file
        // FileWriter fileWritter = new FileWriter(file.getName(),true);
        // BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        // bufferWritter.write("");
        // bufferWritter.close();       

    } catch (Exception e) {
        ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, ToneGenerator.MAX_VOLUME);
        toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);
        e.printStackTrace();
    }
}

日志:

W/System.err(1067): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 W/System.err(1067): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) W/System.err(1067): at java.util.ArrayList.add(ArrayList.java:143) W/System.err(1067): at org.apache.harmony.xml.dom.InnerNodeImpl.insertChildAt(InnerNodeImpl.java:126) W/System.err(1067): at org.apache.harmony.xml.dom.InnerNodeImpl.appendChild(InnerNodeImpl.java:52) W/System.err(1067): at com.demo.xmppchat.XMLReadWrite.writeHistory(XMLReadWrite.java:113)
4

0 回答 0