android studio 断点调试和高级调试

android studio 断点调试和高级调试

有人说Android 的调试是最坑的,那我只能说是你不会用而已,我可以说Android Studio的调试是我见过最棒的。


好了开始写一个简单的调试程序,我们先来一个for循环

1
2
3
4
5
6
7
8
<code  class = "language-java hljs " > for  ( int  i =  0 ; i <  10 ; i++) {
//获取当前i的值
     int  selector = i;
     //打log查看当前i的值(此步多余,实际开发请忽略)
     Logger.e( "for当前的i的值:"  + i);
     //调用方法
     stepNext(i);
}</code>

设置断点(点击红点位置添加或取消断点)

id="iframe_0.9445523186586797" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083835147.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.9445523186586797" frameborder="0" scrolling="no" height="238" style="border-style: none; width: 1027px;">

点击debug模式运行

id="iframe_0.7962600835599005" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083835148.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.7962600835599005" frameborder="0" scrolling="no" height="179" style="border-style: none; width: 1027px;">

查看调试面板

id="iframe_0.1284532891586423" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083835149.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.1284532891586423" frameborder="0" scrolling="no" height="234" style="border-style: none; width: 1027px;">

一、简单调试


1. step over:一步步往下走

id="iframe_0.3612166589591652" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083835150.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.3612166589591652" frameborder="0" scrolling="no" height="74" style="border-style: none; width: 1027px;">

当前程序运行的位置,我们看到i的值已经在程序代码中展示出来了,黄色的代码处,这个是AS的功能,对于我们调试来讲,这简直是非常大的福利了。

id="iframe_0.3540529024321586" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083835151.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.3540529024321586" frameborder="0" scrolling="no" height="236" style="border-style: none; width: 1027px;">

点击单步调试按钮或按快捷键F8,看看效果。这里我们看到selector变量的值已经出来了selector:0,我们在看看黄色位置i的当前值是0。

id="iframe_0.6505639797542244" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083835152.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.6505639797542244" frameborder="0" scrolling="no" height="219" style="border-style: none; width: 1027px;">

这时我们继续F8,我们切换到logcat查看日志,我打印出的i的值是0,

id="iframe_0.7536633932031691" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083835153.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.7536633932031691" frameborder="0" scrolling="no" height="470" style="border-style: none; width: 1027px;">

我们在切回道Debugger面板,可以看到Variables显示面板中,有i的值是0,selector的值是0。以及我们可以看到Frames控制面板中可以显示出当前程序的位置在:onCreate():28,第28行。

id="iframe_0.8491159516852349" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836154.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.8491159516852349" frameborder="0" scrolling="no" height="225" style="border-style: none; width: 1027px;">

2. step into:看到方法往里走

比如我们的for循环当中调用了一个stepNext(int i)方法,当我们走到这里想看看这个方法里面的运行过程的时候我们可以这样,当走到这个方法的时候我们可以按下F7,或者如下图的图标。

id="iframe_0.9210640101227909" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836155.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.9210640101227909" frameborder="0" scrolling="no" height="373" style="border-style: none; width: 1027px;">

这时就走到了stepNext方法当中。

id="iframe_0.6021391001995653" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836156.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.6021391001995653" frameborder="0" scrolling="no" height="384" style="border-style: none; width: 1027px;">

在这里打印了一个log,我们再按一下F8我们来看看Logcat, 这里我打印的log都是为了做教程用,调试我们就不用打log了直接看显示面板就OK了

id="iframe_0.9248606101609766" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836157.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.9248606101609766" frameborder="0" scrolling="no" height="268" style="border-style: none; width: 1027px;">

3. force step into :所有方法看完整

这个是可以看到你所调用的所有方法的实现会让你跟着它走一遍,研究源码使用非常方便

id="iframe_0.9604170080274343" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836158.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.9604170080274343" frameborder="0" scrolling="no" height="416" style="border-style: none; width: 1027px;">

4. step out :有断点下一个,走完断点继续走

这里如果我们的一个流程当中,包括调用的方法,如果有断点走到下一个断点,如果没有断点,而是在一个调用的方法当中,会跳出这个方法,继续走。

id="iframe_0.7099643568508327" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836159.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.7099643568508327" frameborder="0" scrolling="no" height="189" style="border-style: none; width: 1027px;">vc/E0aOsvtm49sD919OjujxiciAvPg0KPGltZyBhbHQ9"这里写图片描述" src="http://www.2cto.com/uploadfile/Collfiles/20150616/20150616083836160.png" title="\" />

(上图)我现在程序位置在第一个断点位置(24行),我调用的stepNext方法中也有一个断点,此时我按下step out按钮会走到stepNext中的断点处(39行)我此时如果再按一下step out 会走到stepNext方法的调用出的下一个可执行代码(30行)

id="iframe_0.38400125154294074" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836161.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.38400125154294074" frameborder="0" scrolling="no" height="396" style="border-style: none; width: 1027px;">

(上图)如果我现在程序位置在stepNext的方法中,如果我此时按下step out,会走到stepNext方法的调用出的下一个可执行代码(30行)

5. run to Cursor :下个断点我们见

这里的意思就是说,会很快执行到下一个断点的位置,而且可以静如任何调用的方法

id="iframe_0.800213945331052" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836162.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.800213945331052" frameborder="0" scrolling="no" height="415" style="border-style: none; width: 1027px;">

二、高级调试


1. 跨断点调试

如果我们设置了多个断点,现在我们需要直接跳转到下一个断点,那么直接点击下图就可以了

id="iframe_0.18753877724520862" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836163.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.18753877724520862" frameborder="0" scrolling="no" height="399" style="border-style: none; width: 1027px;">

2.观察变量

如果我们想观察1个或者几个变量的值的变化,如果我们在Variables显示面版中观察如果我这里有太多太多的自定义变量和系统变量了,那么就难观察了,我们可以做如下操作:
点击Watches,点击+号,然后输入变量的名称回车就OK了,而且会有历史记录哦

id="iframe_0.9682767910417169" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083836164.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.9682767910417169" frameborder="0" scrolling="no" height="235" style="border-style: none; width: 1027px;">

如果变量名比较长我们可以这样:
选择[Variables]中的变量名然后点击[右键],选择[Add to Watches],然后Watches面板中就有了

id="iframe_0.09867034154012799" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083837165.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.09867034154012799" frameborder="0" scrolling="no" height="338" style="border-style: none; width: 1027px;">

3.设置变量的值

在程序中有很多的条件语句和循环语句,调试也是比较耗时的,我们可以通过快速设置变量的值来加快调试速度,我们可以做如下操作:
选择[Variables]中的变量名然后点击[右键],选择[Set Value..]或者选择之后直接F2(如上图)(下图为Variables面板)

id="iframe_0.14501846372149885" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083837166.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.14501846372149885" frameborder="0" scrolling="no" height="229" style="border-style: none; width: 1027px;">

4.查看断点

id="iframe_0.6905952566303313" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083837167.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.6905952566303313" frameborder="0" scrolling="no" height="397" style="border-style: none; width: 1027px;">

点击之后我们可以看到所有的断点,以及位置代码,也可以设置一些属性
id="iframe_0.050346026197075844" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083837168.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.050346026197075844" frameborder="0" scrolling="no" height="634" style="border-style: none; width: 1027px;">

5.停止调试

要注意的是这里的[停止调试]不是让程序停止,而是跳过所有调试
id="iframe_0.03244145284406841" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fwww.2cto.com%2Fuploadfile%2FCollfiles%2F20150616%2F20150616083837169.png&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.03244145284406841" frameborder="0" scrolling="no" height="426" style="border-style: none; width: 1027px;">

到这里我们的Android Studio的断点调试和高级调试就完毕了。

文章来源:http://www.2cto.com/kf/201506/408358.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值