{"record":{"id":"843b4e58d32c1964","repo":"iBotPeaches/Apktool","slug":"parser-must-be-on-start-tag-to-read-next-text","errorCode":null,"errorMessage":"Parser must be on START_TAG to read next text.","messagePattern":"Parser must be on START_TAG to read next text\\.","errorType":"exception","errorClass":"XmlPullParserException","httpStatus":null,"severity":"error","filePath":"brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/BinaryXmlResourceParser.java","lineNumber":549,"sourceCode":"    }\n\n    @Override\n    public int nextToken() throws XmlPullParserException, IOException {\n        return next();\n    }\n\n    @Override\n    public void require(int type, String namespace, String name) throws XmlPullParserException {\n        if (type != mEventType || (namespace != null && !namespace.equals(getNamespace()))\n                || (name != null && !name.equals(getName()))) {\n            throw new XmlPullParserException(TYPES[type] + \" is expected.\", this, null);\n        }\n    }\n\n    @Override\n    public String nextText() throws XmlPullParserException, IOException {\n        if (mEventType != START_TAG) {\n            throw new XmlPullParserException(\"Parser must be on START_TAG to read next text.\", this, null);\n        }\n        int eventType = next();\n        if (eventType == END_TAG) {\n            return \"\";\n        }\n        if (eventType != TEXT) {\n            throw new XmlPullParserException(\"Parser must be on TEXT or END_TAG to read text.\", this, null);\n        }\n        String result = getText();\n        eventType = next();\n        if (eventType != END_TAG) {\n            throw new XmlPullParserException(\"Event TEXT must be immediately followed by END_TAG.\", this, null);\n        }\n        return result;\n    }\n\n    @Override\n    public int nextTag() throws XmlPullParserException, IOException {","sourceCodeStart":531,"sourceCodeEnd":567,"githubUrl":"https://github.com/iBotPeaches/Apktool/blob/79b63384d7d7e22917e6ea8b453272da7012515b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/BinaryXmlResourceParser.java#L531-L567","documentation":"Standard XmlPullParser contract enforced by BinaryXmlResourceParser.nextText(): the parser must currently sit on a START_TAG event. Calling nextText() in any other state (TEXT, END_TAG, END_DOCUMENT, before next()) violates the contract and throws immediately.","triggerScenarios":"Client code calls nextText() without a preceding nextTag()/next() that lands on START_TAG — e.g. looping `while (parser.next() != END_DOCUMENT) { parser.nextText(); }`, or calling nextText() after the loop already consumed the tag.","commonSituations":"Hand-written XmlPullParser traversal code adapted from a DOM/SAX mindset; refactors that inserted an extra next() call; parsers reused across files without reset so event state is stale.","solutions":["Always reach the tag with nextTag() (or check getEventType() == START_TAG) before nextText().","Use the canonical loop: `while ((t = parser.next()) != END_DOCUMENT) { if (t == START_TAG && \"name\".equals(parser.getName())) { String text = parser.nextText(); } }`.","Add an assertion/guard on parser.getEventType() before nextText() during development.","If this fires on apktool's own parsing, the AXML likely has malformed structure — verify with apktool d on a known-good APK."],"exampleFix":"// before\nwhile (parser.next() != XmlPullParser.END_DOCUMENT) {\n    parser.nextText(); // may not be on START_TAG\n}\n\n// after\nint t;\nwhile ((t = parser.next()) != XmlPullParser.END_DOCUMENT) {\n    if (t == XmlPullParser.START_TAG && \"expected\".equals(parser.getName())) {\n        String text = parser.nextText();\n    }\n}","handlingStrategy":"validation","validationCode":"if (parser.getEventType() != XmlPullParser.START_TAG) {\n    throw new IllegalStateException(\"nextText() requires START_TAG, at \" + parser.getPositionDescription());\n}","typeGuard":null,"tryCatchPattern":"if (parser.getEventType() == XmlPullParser.START_TAG) {\n    text = parser.nextText();\n} else {\n    // skip or handle non-tag event\n}","preventionTips":["Land on the element with nextTag() before nextText().","Never call nextText() unconditionally inside a raw next() loop.","Assert event type in debug builds."],"tags":["apktool","xml-pull-parser","api-contract","axml"],"backgroundTag":null,"analyzedSha":"79b63384d7d7e22917e6ea8b453272da7012515b","analyzedAt":"2026-08-14T10:43:28.812Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}