我刚刚开始尝试使用Java进行Android编程来学习Eclipse。事实上,我正在开发developer.android.com教程中的第一个基本示例应用程序。我有以下activity_main.xml:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:orientation="horizontal" >
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
以及以下strings.xml
<?xml version="1.0" encoding="utf-8"? >
<resources>
<string name="app_name">My First App2</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
正如您所看到的,我在strings.xml中有一个button_send字符串和一个edit_message字符串,这两个字符串都在activity_main.xml中引用。但是,构建会产生错误“找不到与给定名称匹配的资源(在'text'处,值为'@ string / button_send')”。有什么想法吗?
答案 0 :(得分:3)
以下是一些想法:
编辑:
您还可以进入eclipse GUI以获取XML文件,然后右键单击该按钮。单击“编辑文本”,这将显示您的字符串值。你应该能看到button_send。如果不这样做,你的strings.xml就会出现问题。
答案 1 :(得分:2)
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:orientation="horizontal" >
你有额外的&gt;在LinearLayout
开头的倒数第二行。删除该角色,清理项目,这可能会解决问题。