Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android XmlPullParser getAttributeCount()始终为-1_Android_Xml_Xmlpullparser - Fatal编程技术网

Android XmlPullParser getAttributeCount()始终为-1

Android XmlPullParser getAttributeCount()始终为-1,android,xml,xmlpullparser,Android,Xml,Xmlpullparser,我试图解析以下XMLstuff.XML以提取每个块中的属性: <?xml version="1.0" encoding="utf-8"?> <MyGroup xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto">

我试图解析以下XML
stuff.XML
以提取每个块中的属性:

<?xml version="1.0" encoding="utf-8"?>
<MyGroup xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto">

    <CompoundSwitch
        android:id="@+id/testing"
        custom:switch_label_tag="label_testing"
        custom:switch_label_text="testing"
        custom:switch_indented="false"
        android:visibility="visible" />

    <CompoundEditText
        android:id="@+id/text_input"
        custom:edit_text_label_tag="label_text"
        custom:edit_text_label_text="text"
        custom:edit_text_label_text_2="postfix"
        custom:edit_text_indented="false"
        android:inputType="text|textMultiLine"
        android:visibility="visible" />

</MyGroup>
但是我发现,
count
总是
-1
,所以我永远无法读取每个块的
属性集


我做错了什么?

只有在事件类型为START\u标记时才应调用attributeCount

以下是我执行xml拉式解析器的代码:

    var eventType = _xml.eventType
    while (eventType != XmlPullParser.END_DOCUMENT) {
        try {
            when (eventType) {
                XmlPullParser.START_TAG -> {
                   val cnt = _xml.attributeCount
                   .......
                }

返回当前开始标记的属性数,如果当前事件类型不是开始标记,则返回-1
。。。在计数之前,您正在执行
parser.next()。。。也许(?)所以你需要在开始标签上读取任何属性?
    var eventType = _xml.eventType
    while (eventType != XmlPullParser.END_DOCUMENT) {
        try {
            when (eventType) {
                XmlPullParser.START_TAG -> {
                   val cnt = _xml.attributeCount
                   .......
                }