2013-03-12 65 views
1

我到处搜索,但无法找到我的问题的答案。我对这个完全陌生,而且在我头脑中。我只是从Android开发站点上做了基本的“我的第一个应用程序教程”。即使代码与示例中的代码完全相同,本教程也会导致错误。在控制台中显示的错误是:Android应用开发我的第一个应用示例错误

[2013-03-12 15:33:36 - MyFirstApp] D:\Android Development\MyFirstApp\res\menu  \main.xml:3: error: Error: No resource found that matches the given name (at 'title' with value '@string/action_settings'). 
[2013-03-12 15:34:38 - MyFirstApp] W/ResourceType(6352): Bad XML block: header size  311 or total size 5346560 is larger than data size 0 

我改变的唯一的东西是教程告诉我要改变的东西。 Activity_main.xmlstrings.xml。我从来没有改变任何东西在main.xml。在Package Explorer它显示一个红色的X旁边MainActivity>onCreate(Bundle)onCreateOptionsMenu(Menu)这里是代码MainActiviy在括号错误

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); (error here red underline under R) 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); (error here red underline under R) 
    return true; 
} 

}

我做了什么错?我刚刚开始学习,我甚至无法让教程正确运行!

这里是activity_main.xml中和strings.xml中,我改变了方向如下:

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

</LinearLayout> 


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

</LinearLayout> 

感谢您的帮助。

+0

请确保在Project.properties中配置的API版本与您的代码相兼容 – 2013-03-12 17:08:26

+0

您确定您的strings.xml是正确的吗?看起来不对我。 – DigCamara 2013-03-12 17:12:04

+0

在Eclipse中,使用“问题”窗口帮助跟踪这样的灾难性故障。当'R无法解析为变量'时,它可以像XML文件中的错字或更复杂的错误资源或库一样简单。忽略在解决根本问题后他们将消失的“R”错误。 – Sam 2013-03-12 17:12:26

回答

2

在控制台日志:

Error: No resource found that matches the given name (at 'title' with value '@string/action_settings

意味着你将需要内部res/values/strings.xml文件添加一个action_settings字符串作为

<resources> 
    <!-- Your other strings --> 
    <string name="action_settings">Action Settings</string> 
</resources> 
0

的代码看起来不错,但XML有一些问题。

  1. 您的代码引用了activity_main,但您使用的文件名是Activity_main。由于匹配区分大小写,Android建议您仅在文件名中使用小写字母。
  2. 您似乎列出了两次Activity_main.xml。 Strings.xml根本就没有。出于这个原因,不可能知道问题是什么。
  3. 您没有列出res/menu目录中的任何文件。如果你正在创建一个选项菜单,你需要res/menu/menu.xml。
0

我自己对Android编程还很陌生,但无论如何都会尽力帮助。

R通常是自动生成的;它下面的红线通常表示它已经消失。

大部分时间你会收到这个错误,因为你在String.xml文件中发生错误。

根据您的错误消息,看起来好像“action_settings”字符串有错误。确保您没有包含任何撇号或发生任何其他语法错误。