android - 尝试了解决方案但没有奏效错误 : Error parsing XML: not well-formed (invalid token)

标签 android xml

我已经尝试了所有方法,但在 Android 中工作时无法解决我的 XML 文件中的这个错误。此外,每当我分配 id 时,我的 R 文件都不会跟踪 id。它总是显示 R 无法解析或不是一个字段,我正在使用的 id 也表示相同。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#d6f5FF">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="vertical">

        <Button 
            android:id="v1"
            android:layout_marginTop="40dp"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="90dip"
            android:layout_marginBottom="15dp"
            android:text="e"
            android:background="@drawable/round_button" />

        <Button 
            android:id="v2"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="90dip"
            android:text="d"
            android:background="@drawable/round_button" />

        <Button 
            android:id="v3"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="90dip"
            android:layout_marginBottom="15dp"
            android:text="c"
            android:background="@drawable/round_button" />

        <Button 
            android:id="v4"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="90dip"
            android:layout_marginBottom="15dp"
            android:text="b"        
            android:background="@drawable/round_button" />

        <Button 
            android:id="v5"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="90dip"
            android:layout_marginBottom="15dp"
            android:text="a"
            android:background="@drawable/round_button" />

    </LinearLayout>

</ScrollView>

最佳答案

您需要为每个id指定@+id/。参见 This post一个很好的解释。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
 android:background="#d6f5FF"  >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="vertical">

    <Button 
        android:id="@+id/v1"
        android:layout_marginTop="40dp"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="90dip"
        android:layout_marginBottom="15dp"
        android:text="e"
        android:background="@drawable/round_button"/>
    <Button 
        android:id="@+id/v2"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="90dip"
        android:text="d"
        android:background="@drawable/round_button"    
        />
    <Button 
        android:id="@+id/v3"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="90dip"
        android:layout_marginBottom="15dp"
        android:text="c"
        android:background="@drawable/round_button"
        />
    <Button 
        android:id="@+id/v4"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="90dip"
        android:layout_marginBottom="15dp"
        android:text="b"        
        android:background="@drawable/round_button"/>               
    <Button 
        android:id="@+id/v5"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="90dip"
        android:layout_marginBottom="15dp"
        android:text="a"
        android:background="@drawable/round_button"/>

    </LinearLayout>

</ScrollView>

关于android - 尝试了解决方案但没有奏效错误 : Error parsing XML: not well-formed (invalid token),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29057802/

相关文章:

android - 如何在类文件中为 TextView 提供文本颜色

java - 无法实例化服务 : ClassNotFoundException

javascript - 如何获取xml中所有的一级嵌套 Node ?

Android - XML 自定义形状

xml - 在哪里可以找到 iText xml 引用?

android - 运行 Android 8.0 的三星设备上的共享元素转换崩溃

Android部分失效在硬件加速的同时绘制整个 View

java.lang.IllegalArgumentException : Username must not be null or empty , AbstractXMPPConnection.java:484

xml - Elixir 和 Erlang 记录模式匹配

java - 如何使用 org.xml.sax.ext.EntityResolver2resolveEntity 方法获取非空名称参数?