如何修复“R.menu.main”在java中首次开发Android应用程序时? [英] How to fix "R.menu.main" in java when first developing an android app?

查看:634
本文介绍了如何修复“R.menu.main”在java中首次开发Android应用程序时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我下载了Android应用程序教程告诉我的所有内容,我使用的是Eclipse,但是我一直在中收到错误消息 R.menu.main

So I downloaded everything that the Android app tutorial told me to, and I am and using Eclipse, but I keep getting an error message on main in R.menu.main:

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);
    return true;

我已导入 android.R; 它不起作用;我删除了那一行,然后我去了 Project> Clean ,它仍然没有自行修复。关于这个主题的其他人一直在说从你的包中导入R文件,但我检查了所有的包和类,没有任何名为 R 。我尝试导入 res 文件夹,但这也无效。
帮助?

I've imported android.R; and it didn't work; I deleted that line and I went to Project>Clean and it still didn't fix itself. Other people on this topic keep saying to import the "R file" from your package but I checked all of the packages and classes and there is nothing called R. I tried importing the res folder and that didn't work either. Help?

推荐答案

你指的是什么Android应用教程?

To what Android app tutorial are you referring?

我会假设你是如此新鲜,无论你转向何方,你都会有一千个问题。在那个阶段,对我来说,诀窍就是得到一些东西,一切都在工作。在那之后,我会继续小心地建立第一次成功(好吧,它的副本,实际上......我是那个偏执狂)。

I'm going to assume that you're so new at this that you have a thousand questions no matter where you turn. The trick for me, at that stage, was to just get something, anything, working. After that, I'd just keep building, carefully, inside of that first success (well, a copy of it, actually... I was that paranoid).

所以...我在这里提供快速带您完成在Android / Eclipse环境中创建应用程序的步骤。如果事情开始崩溃,那么也许你的设置不对。但是如果你的设置很好,那么你应该在大约十分钟内就有一个正在运行的应用程序。

So... I'm offering here to quickly take you through the steps in creating an app in your Android/Eclipse environment. If things start breaking down then perhaps your setup isn't right. But if your setup is good then you should have a running app in about ten minutes.

从那以后,你会发现你不必担心你的Stackoverflow [this]帖子给出的答案中有很多细节(至少,直到你获得更多经验)。

From that, you'll see that you don't have to worry about a lot of the details in the answers given for your Stackoverflow [this] post (at least, not until you gain more experience).

(注意这个东西,更大详细信息,请访问此处,但我会认真对待您让我们开始吧版本)

(note that this stuff, in greater detail, is found here but I'm going to give you a seriously "let's just get this going already" version)

在Eclipse菜单中...

In the Eclipse menus...

文件 - >新的 - > Android应用程序项目

File -> New -> Android Application Project

将出现新的Android应用程序对话窗口

a "New Android Application" dialog window will come up

输入MyTestApp 在应用程序名称字段中,只需接受其他所有内容。
单击下一步。

Type "MyTestApp" in the Application Name field and simply accept everything else. Click Next.

在第一个窗口之后,您将再获得四个对话框窗口。接受每个默认值,然后单击下一步直到进入上一个对话框。在那里,只需单击Finish。

You'll be getting four more dialog windows after that first one. Accept all of the defaults on each of them and simply keep clicking Next until you get to the last dialog. There, just click Finish.

此时,正在创建项目。根据您的计算机,这可能需要几秒钟。您可以在Eclipse窗口的右下角看到消息显示的进度。什么也看不见......它已创建。

At this point, the project is being created. Depending on your computer, this might take a few seconds. You can see progress with the messaging displayed in the bottom right corner of the Eclipse window. See nothing there... it's created.

如果您的Android / Eclipse设置良好,那么您很快就会在Package Explorer中看到MyTestApp项目。项目将打开(您将在层次结构中看到一堆文件夹)。 activity_main.xml将在layout文件夹下突出显示,所有这些都在MyTestApp项目文件夹下。

If your Android/Eclipse setup is good then you should soon see a "MyTestApp" project in the Package Explorer. The project will be open (you'll see a bunch of folders in a hierarchy). "activity_main.xml" will be highlighted under the "layout" folder, all of this under the MyTestApp project folder.

单击MyTestApp项目文件夹,将其选中。然后,在Eclipse菜单中......

Click the MyTestApp project folder, to select it. Then, in the Eclipse menus...

项目 - >构建项目

再次,观察状态消息在Eclipse窗口的右下角。如果您的计算机速度很快,那么您可能无法及时捕捉它们。什么也看不见......你的项目已经建成。

Again, watch the status messaging at the bottom right of the Eclipse window. If your computer is quick then you may not look in time to catch them. See nothing there... your project is built.

在Eclipse菜单中......

In the Eclipse menus...

运行 - >运行

在对话框中选择Android应用程序,然后单击确定。

Select Android Application in the dialog and click OK.

此时,您可能或可能没有获得设备对话框。如果这样做,请选择启动新的Android虚拟设备(模拟器)。您可能需要转到管理器(有一个按钮),以设置设备。我使用AVD_for_Nexus_One_by_Google。我知道一个有效。

At this point, you may or may not get a device dialog. If you do, choose to Launch a new Android Virtual Device (an emulator). You may need to go to the Manager (there's a button for this), to set up a device. I use AVD_for_Nexus_One_by_Google. I know that one works.

预先警告仿真器似乎需要永远启动。耐心点。非常耐心。您应该会看到该应用程序即将运行。

Be forewarned that the emulator will seem to take forever to start up. Be patient. Be VERY patient. You should see the app running shortly.

因此,假设您在模拟器中看到正在运行的应用程序...让我们看看您最终不必担心... 。

So, assuming you see a running application in the emulator... let's see what you ended up not having to worry about...

如果检查自动生成的MainActivity.java文件,在刚刚创建的项目中,您将看到您关注的代码。 。

If you examine the auto-generated MainActivity.java file, in the project that you just created, you'll see that the code that you were concerned about...

getMenuInflater()。inflate(R.menu.main,menu);

getMenuInflater().inflate(R.menu.main, menu);

...似乎精细。没有错误指示。

... seems fine. No error indications.

您还会注意到源代码顶部没有R-type导入。

You'll also notice that there is no "R-type" import at the top of the source code.

注意,如果你深入到gen文件夹的底部,在你的MyTestApp项目文件夹下,在Package Explorer中,有一个R.java文件。这是在构建项目时自动生成的,并且是你的getMenuInflater所引用的文件。

Notice, if you dig down to the bottom of the "gen" folder, under your "MyTestApp" project folder, in the Package Explorer, that there's an R.java file. This is automatically generated when the project is built and is the file to which your getMenuInflater refers.

所有这一切的重点在于你将拥有(如果你的Android) / Eclipse设置很好)工作正常,为您提供了一个与其他东西一起玩的启动点。

The point to all of this is that you'll have (if your Android/Eclipse setup is good) something working, giving you a launch point to play with other things.

祝你好运。我确定爆炸了。

Good luck. I've sure been having a blast.

这篇关于如何修复“R.menu.main”在java中首次开发Android应用程序时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆