【问题标题】:Multiple annotations found at this line: first program Android Programming_ The Big Nerd Ranch Guide f在这一行发现了多个注解:第一个程序 Android Programming_The Big Nerd Ranch Guide f
【发布时间】:2013-12-27 04:08:24
【问题描述】:

我从 Android Programming_The Big Nerd Ranch Guide 书中复制了第一个程序。我收到以下错误。 在此行找到多个注释: - 错误:解析 XML 时出错:文档元素后出现垃圾 - 根元素之后的文档中的标记必须是正确的 - 形成。 在第 16 行。 这是我的代码

<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"
tools:context=".QuizActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

</RelativeLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"  >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/question_text" />

<LinearLayout
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    andriod:orientation="horizontal" >
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/true_button" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/false_button"   />

</LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您正试图在同一个 xml 中使用两个父布局。这在 Android 中是不可能的。在这种情况下,使用RelativeLayoutLinearLayout 作为父级。

    使用此模板:

    <RelativeLayout
        //relative layout attributes
        >
        //you can add as many as elements here
    
        <LinearLayout
            //linear layout attributes
            >
            //you can add as many as elements here
        </LinearLayout>
    </RelativeLayout>
    

    如果你想同时使用RelativeLayoutLinearLayout,在xml文件中添加一个单独的父布局,像这样:

    <RalativeLayout
        //this is your main parent layout
        >
    
        //Add as many as Linear/Relative layout here doesn't matter.
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      更改布局:您正在尝试为一个 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"
      tools:context=".QuizActivity" >
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:text="@string/hello_world" />
      
      
      
      <LinearLayout 
      
      android:layout_width="match_parent"
      android:layout_height="match_parent"
       //u can specify `below` , `above` property as per your need
      android:gravity="center"
      android:orientation="vertical"  >
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="24dp"
          android:text="@string/question_text" />
      
      <LinearLayout
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"  
          andriod:orientation="horizontal" >
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/true_button" />
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/false_button"   />
      
      </LinearLayout>
      
      </LinearLayout>
      </RelativeLayout>
      

      【讨论】:

      • 谢谢。在书中,它的编写方式与我发布的方式相同。那么为什么它向我显示错误?书有错吗?
      • 在书中检查两次。它可能是两种不同的布局。可能是你把它写成一个完整的布局。
      【解决方案3】:
      // try this way
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout 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"
          android:gravity="center"
          android:orientation="vertical"
          tools:context=".QuizActivity"  >
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="24dp"
              android:text="@string/question_text" />
      
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/true_button" />
              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/false_button"   />
      
          </LinearLayout>
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多