14

我正在使用 eclipse 来编写一个 android 应用程序,但我停了下来。我尝试关闭我的代码

<?xml version="1.0" encoding="utf-8"?>

但我不断收到错误消息

“不允许与“[xX][mM][lL]”匹配的处理指令目标。”

这是代码:

<?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:orientation="horizontal" >

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />

</EditText>  

<resources>
    <string android:name="app_name"></string>
    <string android:name="edit_message"></string>
    <string android:name="button_send"></string>
    <string android:name="action_settings"></string>
    <string android:name="title_activity_main"></string>
</resources>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />

<EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />

</EditText> 
<?xml version="1.0" encoding="utf-8"?>

我犯了什么错误吗?如果我这样做了,请通知我。

4

7 回答 7

17

请确保在<?xmlxml 文件的开头之前不应有任何字符。希望对你有帮助

于 2014-06-06T11:56:53.633 回答
11

是的,那个 xml 列表有几个错误(假设它只是一个文件)。

删除这些字符串资源并将它们strings.xml放在项目res/values文件夹中自己的文件中。此外,该 strings.xml 文件也应该以它自己的开头<?xml version="1.0" encoding="utf-8"?>

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string android:name="app_name"></string>
<string android:name="edit_message"></string>
<string android:name="button_send"></string>
<string android:name="action_settings"></string>
<string android:name="title_activity_main"></string>
</resources>

这不是错误,但您可能希望向这些字符串资源添加一些可能的文本标签,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string android:name="app_name">My Application Name</string>
<string android:name="edit_message"></string>
<string android:name="button_send">Send</string>
<string android:name="action_settings"></string>
<string android:name="title_activity_main"></string>
</resources>

不要忘记关闭您的 LinearLayout :

</LinearLayout>

删除两个 requestFocus 中的一个,我会假设您应该每个布局只有一个(再次假设,您只想向我们显示一个文件而不是多个文件)。

<requestFocus />

并删除最后一行(这是 Eclipse 抱怨的):

<?xml version="1.0" encoding="utf-8"?>

您确实保留了 xml 文件的第一行中的那个,但是您从最后一行中删除了那个,因为这种类型的指令不会关闭。

让我知道这是否解决了您的所有问题。我不能确定它确实如此,因为我没有花时间打开我的另一台计算机并检查 Eclipse。

于 2013-11-12T01:13:42.893 回答
3

是的,之前不应该有任何字符<?xml..甚至没有空白字符..我遇到了同样的问题..得到了解决..

于 2014-07-25T10:13:48.663 回答
0

打开 manifest.xml ctrl+A,然后按ctrl+I。它将自动格式化您的 manifest.xml。

于 2015-04-02T15:42:20.440 回答
0

1) 在 XML 文件中使用或标记时,请确保您的代码不包含任何未声明的变量(在 string.xml 文件中)

2) 确保“R.java”文件反映了所有使用的变量。

3)重新构建您的项目并验证。

于 2014-02-25T10:22:19.270 回答
0

如果语法正确并且仍然出现此错误,则只需打开 xml 文件并右键单击并选择验证。

于 2016-05-18T09:24:51.983 回答
0

1.确保你已经写在清单文件的开头... 2.确保文件之前不应该有任何字符...

于 2016-05-19T11:34:17.603 回答