【问题标题】:errors in hello world xml你好世界xml中的错误
【发布时间】:2013-06-24 16:49:05
【问题描述】:

我是 android 开发的新手,正在为它读一本书(开始 android 4 开发),在创建 hello world 时,我遇到了一些错误,首先我解决了它们,但我的应用程序被强制关闭,所以我复制粘贴了所有东西并得到 4 个错误:-

在这一行找到多个注释: - 属性缺少 Android 命名空间前缀 - 可疑命名空间:应以 http:// 开头 - 与元素类型“LinearLayout”关联的属性“xmlns:android”需要打开引用。 - 错误:解析 XML 时出错:格式不正确(令牌无效)

为标签 TextView 找到了意外的命名空间前缀“android”

在这一行找到多个注释: - 为标签 TextView 找到了意外的命名空间前缀“android” - 属性缺少 Android 命名空间前缀

为标签 Button 找到了意外的命名空间前缀“android”

代码示例

<?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:layout_orientation="vertical"

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

 <TextView
     android:layout_width=”fill_parent”
     android:layout_height=”wrap_content”
     android:text=”@string/This is my first Android Application!” />

 <Button
     android:layout_width="fill_parent"
     android:layout_width="wrap_content"
     android:text="@string/And this is clickable :D" />

我不认为代码有任何错误:/

【问题讨论】:

  • 上面的xml和书中的一样吗,我真的很怀疑。

标签: java android xml


【解决方案1】:

这是一个复制粘贴问题。用 " 替换第 2 行中的引号,它应该可以工作

【讨论】:

  • 去掉第 2 行“res/”和“android”之间的空格 字符串名称不能有空格。对于所有 android:text 属性删除 "@string/"
【解决方案2】:

将每行代码的引号替换为“并使用ctr+sift+f并保存文件。

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

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

 <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/This is my first Android Application!" />

 <Button
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/And this is clickable :D" />

</LinearLayout>

【讨论】:

    【解决方案3】:

    在您的 xml 中将 ” 替换为 "" 以使其成为有效的 xml

    【讨论】:

      【解决方案4】:
      apk/res/ android
      

      删除此处的空间,使用

      apk/res/android
      

      【讨论】:

        【解决方案5】:

        @string 资源应该在 strings.xml 中声明,试试这个!并阅读更多

        <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        
             <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="hello" />
        
             <TextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="This is my first Android Application!" />
        
             <Button
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="And this is clickable :D" />
        
            </LinearLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-19
          • 1970-01-01
          相关资源
          最近更新 更多