Java Android:错误:解析 XML 时出错:格式不正确(令牌无效)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19723108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:41:45  来源:igfitidea点击:

Android: error: Error parsing XML: not well-formed (invalid token)

javaandroidxml

提问by Marius

There's an error in my activity_main.xml on line 13 (Wich you can see below). It says "Error parsing XML: not well-formed(invalid token)". In addition to that my R.java has killed itself. I've found ways to fix it, but they won't work (At least I think it's because of that) until this error is fixed. Thanks for any help. Just by the way I'm following the tutorial on Javacodegeeks.com

我的 activity_main.xml 中的第 13 行有一个错误(您可以在下面看到)。它说“解析 XML 时出错:格式不正确(无效令牌)”。除此之外,我的 R.java 已经自杀了。我已经找到了修复它的方法,但在修复此错误之前它们将无法工作(至少我认为是因为这个)。感谢您的帮助。顺便说一下,我正在关注Javacodegeeks.com上的教程

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:mynamespace="http://schemas.android.com/apk/res/com.Learning.learning"
            xmlns:tools="http://schemas.android.com/tools"
            mynamespace:layout_width="success"
            mynamespace:orientation="vertical"
            mynamespace:layout_height="success">

            <ListView   android:id="@+id/listView"
                        android:layout_height="match_parent"
                        android:layout_width="match_parent"/>

                    List<Map<String>> planetsList = new ArrayList<Map<String,String>>() />

                        private void initList() {
                            //we populate them Planets

                            planetsList.add(createPlanet("planet", "Mercury"));
                            planetsList.add(createPlanet("planet", "Venus"));
                            planetsList.add(createPlanet("planet", "Earth"));
                            planetsList.add(createPlanet("planet", "Mars"));
                            planetsList.add(createPlanet("planet", "Jupiter"));
                            planetsList.add(createPlanet("planet", "Saturn"));
                            planetsList.add(createPlanet("planet", "Uranus"));
                            planetsList.add(createPlanet("planet", "Neptune"));
                        }

                        private HashMap<String, String> createPlanet(String key, String name) {
                            HashMap<String, String> planet = new HashMap<String, String>();
                            planet.put(key, name);

                            return planet;
                        }

                        // This is a simple adapter that accepts as parameter
                        // Context
                        // Data List
                        // The row layout that is used during the row creation
                        // The keys used to retrieve the data
                        // The View id used to show the data. The number and the view must match
                        simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String []
                        {"planet"}, new int[] {android.R.id.text1});

                    </ListView>

采纳答案by aj_shehan

Have you mixed the Java code in the activity_main.xml too? Because your activity_main.xml should only contain the following code:

您是否也在 activity_main.xml 中混合了 Java 代码?因为您的 activity_main.xml 应该只包含以下代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView android:id="@+id/listView" 
          android:layout_height="match_parent"
          android:layout_width="match_parent"/>

</RelativeLayout>

And MainActivity.java should contain the following code:

而 MainActivity.java 应包含以下代码:

List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();

private void initList() {

planetsList.add(createPlanet("planet", "Mercury"));
planetsList.add(createPlanet("planet", "Venus"));
planetsList.add(createPlanet("planet", "Mars"));
planetsList.add(createPlanet("planet", "Jupiter"));
planetsList.add(createPlanet("planet", "Saturn"));
planetsList.add(createPlanet("planet", "Uranus"));
planetsList.add(createPlanet("planet", "Neptune"));

}

private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.put(key, name);

return planet;
}

I have just copied the code here from the link that you have provided.

我刚刚从您提供的链接中复制了此处的代码。

回答by Satyen Udeshi

Remove your JavaCode from xml and put it in seperate java file you cannot put java code inside your xml file.

从 xml 中删除您的 JavaCode 并将其放在单独的 java 文件中,您不能将 java 代码放在 xml 文件中。

回答by jfv

Are you putting Java code inside the XML file? Java code goes inside a class file.

您是否将 Java 代码放入 XML 文件中?Java 代码位于类文件中。

According to that tutorial, they want you to create a xml file called activity_main.xmlwith contents

根据该教程,他们希望您创建一个名为activity_main.xml内容的 xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView android:id="@+id/listView" 
              android:layout_height="match_parent"
              android:layout_width="match_parent"/>

</RelativeLayout>

And separatelly, a java class called MainActivity.java

并且单独地,一个名为的 java 类 MainActivity.java

with contents

有内容

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // ....
    }
//...
}

回答by Naveen Kumar Alonekar

from your posted link source

来自您发布的链接

Remove Javacode inside XML tags

删除 XML 标签内的 Javacode

And

I think change it as

我认为将其更改为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:mynamespace="http://schemas.android.com/apk/res/com.Learning.learning"
            xmlns:tools="http://schemas.android.com/tools"
            mynamespace:layout_width="success"
            mynamespace:orientation="vertical"
            mynamespace:layout_height="success">

            <ListView   android:id="@+id/listView"
                        android:layout_height="match_parent"
                        android:layout_width="match_parent" /> 
</RelativeLayout>                                           ^Listview tag closed here

Remove </ListView>tag. Because you are already closed at 12th line

移除</ListView>标签。因为你已经在第 12 行关门了

回答by Bhoot

You have included java code in the XML file. Remember that Android uses XML files for layouts and resources definition only. Code for data poplulation, manipulation and other processing must be done in the java source files. For example, you can populate the list within you MainActivity.java file.

您已经在 XML 文件中包含了 java 代码。请记住,Android 仅将 XML 文件用于布局和资源定义。数据填充、操作和其他处理的代码必须在 java 源文件中完成。例如,您可以填充 MainActivity.java 文件中的列表。

回答by majk

You can't write your program code in your XML file. That is why you have an error. Read more about XML Layout

您不能在 XML 文件中编写程序代码。这就是为什么你有错误。阅读有关XML 布局的更多信息