【问题标题】:No XML content. Please add a root view or layout to your document没有 XML 内容。请为您的文档添加根视图或布局
【发布时间】:2012-04-28 04:37:24
【问题描述】:

我正在开发一个安卓应用程序。我是 XML 新手。我收到消息说“没有 XML 内容。请为您的文档添加根视图或布局” XML 代码如下所示。请帮忙

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ref" 
        android:textSize="15dp"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ref2" 
        android:textSize="15dp"/>

    <EditText
        android:id="@+id/uname"
        android:layout_width="150dp"
        android:layout_height="25dp"
        android:inputType="text"
        android:ems="5" >

        <requestFocus />
    </EditText>

   <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ref3" 
        android:textSize="15dp"/>

    <EditText
        android:id="@+id/pwd"
        android:layout_width="150dp"
        android:layout_height="25dp"
        android:inputType="text"
        android:ems="5" />

    <Button
        android:layout_width="60dp" 
        android:layout_height="35dp"
        android:text="@string/ref" 
        android:id="@+id/bLogin" />
  </LinearLayout>

----主要活动代码---

package com.android.disasterAlertApp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends Activity {
   /** Called when the activity is first created. */
   Button login;
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    login =(Button) findViewById(R.id.bLogin);
   }
}

----Manifest----
[2012-04-17 00:34:20 - Disaster Alert App] res\layout\main.xml:0: error: Resource entry      main is already defined.
[2012-04-17 00:34:20 - Disaster Alert App] res\layout\main.out.xml:0: Originally defined here.
[2012-04-17 00:34:20 - Disaster Alert App] C:\Users\Acer\workspace\Disaster Alert App\res\layout\main.out.xml:1: error: Error parsing XML: no element found
[2012-04-17 01:05:59 - Disaster Alert App] res\layout\main.xml:0: error: Resource entry main is already defined.
[2012-04-17 01:05:59 - Disaster Alert App] res\layout\main.out.xml:0: Originally defined here.
[2012-04-17 01:05:59 - Disaster Alert App] C:\Users\Acer\workspace\Disaster Alert App\res\layout\main.out.xml:1: error: Error parsing XML: no element found
[2012-04-17 01:11:14 - Disaster Alert App] res\layout\main.xml:0: error: Resource entry main is already defined.
[2012-04-17 01:11:14 - Disaster Alert App] res\layout\main.out.xml:0: Originally defined here.
[2012-04-17 01:11:14 - Disaster Alert App] C:\Users\Acer\workspace\Disaster Alert App\res\layout\main.out.xml:1: error: Error parsing XML: no element found
[2012-04-17 01:14:56 - Disaster Alert App] ------------------------------
[2012-04-17 01:14:56 - Disaster Alert App] Android Launch!
[2012-04-17 01:14:56 - Disaster Alert App] Connection with adb was interrupted.
[2012-04-17 01:14:56 - Disaster Alert App] 0 attempts have been made to reconnect.
[2012-04-17 01:14:56 - Disaster Alert App] You may want to manually restart adb from the Devices view.
[2012-04-17 01:17:27 - Disaster Alert App] Error in an XML file: aborting build.
[2012-04-17 01:37:10 - Disaster Alert App] res\layout\main.xml:0: error: Resource entry main is already defined.
[2012-04-17 01:38:42 - Disaster Alert App] res\layout\main.xml:0: error: Resource entry main is already defined.
[2012-04-17 01:38:42 - Disaster Alert App] res\layout\main.out.out.xml:0: Originally defined here.
[2012-04-17 01:38:42 - Disaster Alert App] C:\Users\Acer\workspace\Disaster Alert App\res\layout\main.out.out.xml:1: error: Error parsing XML: no element found
[2012-04-17 01:39:28 - Disaster Alert App] Error in an XML file: aborting build.

【问题讨论】:

  • 当您发布代码时,请确保在您的问题中进行(如果需要,有一个编辑按钮)代码不会在 cmets 中格式化,因此更难遵循。
  • 你的xml文件叫main.xml吗?它是否位于您的 res/layout/ 文件夹中?
  • 我已经发布了主要活动和清单。是的,这是它的名称和位置。

标签: android


【解决方案1】:

哦——看到“main.out.xml”了吗?当您使用 Eclipse 启动 XML 文件时会发生这种情况。只需删除'out' xml 并保留 main.xml

当您点击 Play 时,请务必选择/正在编辑一个 .java 文件,否则 eclipse 将生成一个“out.xml”并且无法启动。

【讨论】:

  • 看起来您也有多个文件,请删除布局文件夹中名称中包含“out”的所有文件。此页面有一个修复程序,将阻止生成 out.xml 文件:stackoverflow.com/questions/2393103/…。这也不是清单...清单位于项目根目录的 android-manifest.xml 文件中。在此处阅读以了解有关它们的更多信息:developer.android.com/guide/topics/manifest/manifest-intro.html
  • 谢谢你,Tim - 当我第一次遇到那个错误时感到非常沮丧,我已经训练自己在大量编辑 XML 并需要测试时关闭到一个类文件来启动。
【解决方案2】:

我想指出 ADT Preview 21 中存在一个错误 - 我在此处安装了预览工具来修复 MacBook Retina 模拟器错误:Android Emulator do not scale to 100% for Mac Book Pro with Retina Display

但是,我现在无法打开任何布局,因为它们都给了我这个错误。我发现有一种解决方法:切换到 XML 视图并进行任何编辑——即使它是一个空格。保存,再切换回图形布局,错误就消失了。

【讨论】:

    【解决方案3】:

    只需关闭 Eclipse 并重新启动。这对我有用。

    【讨论】:

      【解决方案4】:

      为我工作:在 Eclipse 中单击帮助->检查更新。 如果你有可用的更新(特别是 Android 的东西)- 安装它们。

      【讨论】:

        猜你喜欢
        • 2012-06-30
        • 2010-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-21
        • 2021-03-28
        • 1970-01-01
        相关资源
        最近更新 更多