51testing 2007-12-29 10:48
Android程序开发初级教程(三)
[font=宋体][b][size=5]调试你的项目[/size][/b][/font]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]这个用于[/font][font=Times New Roman]Eclipse[/font][font=宋体]中的[/font][font=Times New Roman]Android[/font][font=宋体]插件作为[/font][font=Times New Roman]Eclipse[/font][font=宋体]中的调试器也具有优秀的兼容性。要演示这些,让我们制造一个[/font][font=Times New Roman]bug[/font][font=宋体]在代码中,改变你的[/font][font=Times New Roman]HelloAndroid[/font][font=宋体]源代码象下面这样:[/font][/size]
[font=Times New Roman][size=3] [/size][/font]
[size=3][color=#004000]public class HelloAndroid extends Activity {[/color][/size][size=3][color=#004000]
/** Called when the activity is first created. */[/color][/size][size=3][color=#004000]
@Override[/color][/size][size=3][color=#004000]
public void onCreate(Bundle icicle) {[/color][/size][size=3][color=#004000]
super.onCreate(icicle);[/color][/size][size=3][color=#004000]
Object o = null;[/color][/size][size=3][color=#004000]
o.toString();[/color][/size][size=3][color=#004000]
setContentView(R.layout.main);[/color][/size][color=#004000][size=3]
}[/size][/color][color=#004000][size=3]}[/size][/color]
[font=Times New Roman][size=3] [/size][/font]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]这次简单的变化会引起一个[/font][font=Times New Roman]NullPointerException[/font][font=宋体]异常,[/font][font=Times New Roman] [/font][font=宋体]如果你再次运行程序,你将会看到下面的屏幕:[/font][/size]
[font=Times New Roman][size=3] [img]http://p.blog.csdn.net/images/p_blog_csdn_net/k7sem/hello_world_8.png[/img][/size][/font]
[size=3][font=宋体]要找到什么地方出错,需要设置个断点在你源代码的[/font][font=Times New Roman] [/font][font=Times New Roman]“Object o = null;” [/font][font=宋体]行后[/font][font=Times New Roman]([/font][font=宋体]你可以双击在[/font][font=Times New Roman]Eclipse[/font][font=宋体]中显示行数的左部区域[/font][font=Times New Roman]), [/font][font=宋体]然后选择[/font][font=Times New Roman] Run -> Debug [/font][font=宋体]选择最后一次的运行加载。[/font][font=Times New Roman] [/font][font=宋体]你的程序将会重起模拟器,但是这个时候它会挂起,当它走到你刚才设置的断点的时候,在[/font][font=Times New Roman]Eclipse[/font][font=宋体]的调试模式视图中,它就会停止在你的代码处。就像你可以在其他任意程序中做这个一样。[/font][/size][size=3]。[/size]
[size=3][font=宋体][img]http://p.blog.csdn.net/images/p_blog_csdn_net/k7sem/hello_world_9.png[/img][/font][/size]
[font=Times New Roman][size=3] [/size][/font][font=Times New Roman][size=3] [/size][/font]
[font=Times New Roman][size=3] [/size][/font]
[font=Times New Roman][size=3] [/size][/font]
[b][size=5][font=宋体]使用其它的IDE工具[/font][font=宋体]创建项目[/font][/size][/b]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]如果你没有使用[/font][font=Times New Roman]Eclipse([/font][font=宋体]比如你使用其他的[/font][font=Times New Roman]IDE[/font][font=宋体]开发工具,或者简单的只使用文本编辑和命令行工具[/font][font=Times New Roman])[/font][font=宋体], 那么这个插件不会帮上你忙,别担心,你不会因为没有使用[/font][font=Times New Roman]Eclipse[/font][font=宋体]而丢失任何开发能力。[/font][/size]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]其实[/font][font=Times New Roman]Android[/font][font=宋体]的[/font][font=Times New Roman]Eclipse[/font][font=宋体]插件也是将一套[/font][font=Times New Roman]Android SDK[/font][font=宋体]的开发工具包装了。[/font][font=Times New Roman]([/font][font=宋体]这些工具像:模拟器,[/font][font=Times New Roman]aapt, adb, ddms, [/font][font=宋体]和其他的,可以参考[/font][font=Times New Roman]google[/font][font=宋体]的相关文档[/font][font=Times New Roman])[/font][font=宋体],[/font][font=Times New Roman] [/font][font=宋体]因此,[/font][font=Times New Roman] [/font][font=宋体]它也可以被其他工具包装,比如[/font][font=Times New Roman]ant.[/font][/size]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]在[/font][font=Times New Roman]Android SDK[/font][font=宋体]中包含一个[/font][font=Times New Roman]Python[/font][font=宋体]脚本,叫做[/font][font=Times New Roman]”activityCreator.py”[/font][font=宋体],[/font][font=Times New Roman] [/font][font=宋体]它用于为了你的项目创建所有的源代码和目录的编译环境[/font][font=Times New Roman]([/font][font=宋体]个人理解[/font][font=Times New Roman]), [/font][font=宋体]也就是会产生可用于[/font][font=Times New Roman]ant[/font][font=宋体]编译的[/font][font=Times New Roman]build.xml. [/font][font=宋体]这样就允许你的项目从命令行方式创建或者集成到你选择的[/font][font=Times New Roman]IDE[/font][font=宋体]中。[/font][/size]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]比如,要创建一个[/font][font=Times New Roman]HelloAndroid[/font][font=宋体]项目,就像刚才我们用[/font][font=Times New Roman]Eclipse[/font][font=宋体]中的一样,[/font][font=Times New Roman] [/font][font=宋体]你可以使用下面的命令:[/font][/size]
[font=Times New Roman][size=3] [/size][/font]
[size=3][color=#004000]activityCreator.py --out HelloAndroid com.google.android.hello.HelloAndroid[/color][/size]
[font=Times New Roman][size=3] [/size][/font]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]要编译你的项目,你要接着使用[/font][font=Times New Roman]ant[/font][font=宋体]命令,当命令行提示成功时候,你会看到一个名为[/font][font=Times New Roman]HelloAndroid.apk[/font][font=宋体]的文件在[/font][font=Times New Roman]”bin”[/font][font=宋体]目录下。[/font][font=Times New Roman] [/font][font=宋体]这个[/font][font=Times New Roman].apk[/font][font=宋体]文件是一个[/font][font=Times New Roman]Android[/font][font=宋体]的包,[/font][font=Times New Roman] [/font][font=宋体]它需要使用[/font][font=Times New Roman]”adb”[/font][font=宋体]工具安装进模拟器。[/font][/size]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]到这里为止,我向大家展示了[/font][font=Times New Roman]Android[/font][font=宋体]平台的程序简单的开发实例,本人水平有限,翻译的时间仓促,一定有很多错误和疏漏,请多提修改意见。本系列教程的主要内容都是翻译自[/font][font=Times New Roman]Google[/font][font=宋体]的[/font][font=Times New Roman]Android[/font][font=宋体]开发文档。通过下面的链接查看全部内容。[/font][/size]
[url=http://code.google.com/android/intro/hello-android.html][font=Times New Roman][size=3][color=#0000ff]http://code.google.com/android/intro/hello-android.html[/color][/size][/font][/url]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=宋体]希望[/font][font=Times New Roman]Android[/font][font=宋体]平台能在移动开发领域开启新的篇章。[/font][/size]