This article directory
Overview
As we all know, breakpoint debugging is a necessary skill for a qualified programmer. Breakpoint debugging can help us view the values of each variable during each step of the program. Learning to debug can help us save a lot of time for troubleshooting. Let's take a look at the methods and techniques of debugging in Android Studio.
There are two debugging modes in Android Studio which are
- Debug mode
- Attach mode
There is no big difference in the use of the two modes. Debug mode directly clicks the button to run debugging. In Attach mode, after the program starts, click the application that needs to be debugged.
Debugging method
Step 1: add test code
We first MainActivity .java
add some code in
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add(getSting(i));
setInto(i);
Log.i("MainActivity", "这是第" + i + "条数据");
}
}
public String getSting(int i) {
return ("第" + i + "条数据");
}
public void setInto(int i) {
Log.i("MainActivity", "i=" + i);
}
}
Step 2: add breakpoints
During debugging or before debugging, click on the left side of the editing area to
add a debug breakpoint. Click again on a debug breakpoint that has been added to cancel the line breakpoint
Step 3: Commissioning
show Execution point
功能:显示执行点:点击该按钮,光标将定位到当前正在调试的位置
快捷键:Alt + F10
Step Over
功能:单步跳过,点击该按钮表示程序将执行下一行,如果该行是一句代码直接执行到下一行,如果该行是
一个方法, 不会进入该方法,直接执行到下一行。
快捷键:F6
Step Into
功能:单步跳入,点击该按钮表示程序将执行下一行,如果该行是一个方法, 会进入该方法继续执行。
快捷键:F5
Force Step Into
功能:单步强制跳入,点击该按钮表示程序将执行下一行,如果该行是一个方法, 会进入该方法继续执行。
该操作和前面的Step Into的区别在于:Step Into 遇到系统方法不会强制进入,该方法遇到系统方法也
会强制进入。
快捷键:Alt + shift + F7
Step Out
功能:跳出前面step into 进入的方法,如果需要跳出的该方法有断点就不会跳出该方法,而会进入该方法下个断点
快捷键:F7
run to Cursor
功能:跳到下一个断点
快捷键:Ctrl + R
This function can also be achieved by clicking the green button on the left
View breakpoint
If we want to see where the breakpoints are hit, click this button to
uncheck the breakpoints
Modify variable value
In the variable area, right-click set value to modify the value during variable operation
Stop debugging
If you want to end the debugging process, click this button