元素类型“按钮”必须后跟属性规范,“>”或“/>”

时间:2013-06-28 18:24:29

标签: android xml eclipse

我在XML中创建了一个按钮,这里是创建参数

<Button android:id="@+id/btn2"
  --->  android:layout_width="wrap_content"  <----
    android:layout_height="wrap_content"
    android:text="@string/PNR"
/>

我收到了错误并且表示错误:

“元素类型”按钮“必须后跟属性规范”,“>”或“/&gt;”“

不仅在按钮ID中我尝试创建TextView,那么同样的错误也会出现在同一个地方。

我已经检查了之前的帖子,但是他们说这些标签没有关闭,对我没用。

请建议我,该怎么办?这是完整的代码:

<?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" >

<Button android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/PNR"
    />

</LinearLayout>

1 个答案:

答案 0 :(得分:5)

尝试清理项目

  

项目 - &gt;清洁......然后选择你的项目

有时Eclipse不会对您的xml进行更改。当你遇到这样的愚蠢错误时,请先尝试清洁。有时,在更改xml中的内容(如

)后立即运行时,您将获得Java代码ClassCastException
  

无法将Button转换为EditText

或类似的东西没有意义。这也是清理项目的好时机。

我还建议摆脱元素中的空白,因为我也遇到了麻烦(特别是在Eclipse的旧版本上),而且我觉得它看起来更干净。所以我会将您的<Button...更改为

<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PNR"/>  <!-- just moved your end tag to this line -->
相关问题