配置完Android开发环境后,遇到两个问题,一个属于非技术问题,另一个属于技术问题。
先说非技术问题。
很简单,启动Android模拟器(需要先创建AVD)时,先看到的是一个文本界面,我一开始以为自己的配置出了什么问题。迷惑了大半天,晚上从外面吃饭回来,突然想起来《Android基础教程》(人民邮电出版社,2009年11月)中有一段提示:“启动模拟器需要花较长时间。可以这样想象一下——首次开机时,手机也需要启动,就像任何计算机系统一样。关闭模拟器就像是关闭手机或取出手机电池一样。会不会是我太着急了?应该有点耐心才好。于是,我重新启动模拟器,耐心等待……大约3分钟后,终于看到Android的图形用户界面,OK。
正好《Beginning Android 2》这本书中也有一段相关的话:NOTE: The first time you use an AVD with the emulator, it will take substantially longer to start than it will subsequent times.(注意:第一次使用AVD来启动模拟器的时间会比较长,后续的启动速度会有所提升。)
再说技术问题。
前面只是解决了启动模拟器的问题,接下来就是要在模拟器中实际地加载新应用程序并进行测试。但是,我新创建了FirstApp应用程序,在通过Eclipse运行该项目时(也可以在命令行中使用ant构建项目,然后运行android命令,再启动模拟器;不过,这需要再下载其他软件包),提示出错,错误信息如下:
1. Project “FirstApp is missing required source folder: ‘gen’
2. The project could not be built until buid path errors are resolved.
在网上搜索到几个解决方案(列在下面,供朋友们参考)。但奇怪的是,在刚搜索到第一个方案时,还没等到采取任何措施,Eclipse中的错误居然自动消失了(FirstApp项目下方的红叉也不见了),再Run as Android Application,一切正常了。我想,也许正如第三个方案中某人所说的,Eclipse并不能实时检测到OS文件系统的变化(编译项目时,会生成新文件),这也许就是导致这个技术问题的原因——至于是不是这个原因,还有待于进一步求证。
一、右击项目,选择preferences->builder,在右边的configure一栏中将Android Packege Builder一项提到Java Builer之前
出处:http://www.androidin.net/bbs/thread-708-11-1.html
二、将Eclipse自动生成的R.java删掉,刷新项目,R.java便会重新生成
出处:http://www.blogjava.net/crazycoding/archive/2010/03/27/316701.html
三、在项目文件夹中新创建一个Java类或者直接修改自动生成的类文件
出处:http://www.coderanch.com/t/466092/Android/Mobile/android-eclipse
以下是几个人的回复,感觉这种讨论的技术氛围很不错。今天太晚了,明天天亮还要去平谷,回来再翻译。
James Dixon的回复
Hi Divya
I’ve had the same problem as well. I think the issue is that the project creation does not initiate a build when it finishes, so you need to make a change, and save for it to generate the gen folder.
For me creating a new java class seemed to do the trick, but I’d imagine just making a change to a file and saving should work too.
Robert F. Howard的回复
I just ran into this, too. I am following the example in Hello Android, which I assume is what the others in this thread were doing. James’s solution (editing the source file) worked for me, so thank you for that.
So your solution is good, but I don’t think I totally believe the diagnosis. The thing is, the directory actually did exist before I edited the file and rebuilt. This is my first time using Eclipse, and it’s very disappointing. The error message should specify the full path of the directory it wants, and then it should be possible to create the directory and re-build, but it doesn’t work until the source file is edited. It makes me wonder what is really going on inside Eclipse.
Tim Holloway的回复
This seems to be a small glitch courtesy of Eclipse’s distancing itself from the OS filesystem (which is why Eclipse has an explicit Refresh command).
The gen folder and the “R.java file are built by one of the Android utilities. The Eclipse Android plugin invokes this app, but it doesn’t always know when it needs to. I have similar problems when I want to define a new resource ID. Since I can’t seem to get the GUI resource ID definer to enable itself, I just create new IDs in the resource files themselves. But unless I trigger the android resource generator, they don’t get inserted into “R.java.
And you don’t want to manually insert into “R.java, because when the resource compiler does fire off, your code mods will be overwritten.
One way to force the issue is to select the Project/Clean menu command. If you have the automatic build switched off, you’ll then have to initiate a build. Otherwise the clean will fire off the auto-build process.