Android Studio practical debugging skills, androidstudio
Android Studio is an excellent development tool, and its built-in debugging environment is very powerful. We need to get rid of inefficient methods that only use Log to print logs, mastering Advanced debugging skills is necessary for every Android developer.
Debugging method:Use the following method for debugging:
Run debugging: Click the gear running button. The debugging window is displayed in IDE;
Additional process: If the App is running, click the "bug" icon and select the App process to debug;
After successful operation, the following DEBUG floating bar appears. Next, you can start to set breakpoints.
Start debugging:
Click the left button on the code line to automatically run the program to the breakpoint;
Step by step: It is the core and essence of debugging. It is easy to get the desired data. Open the Run menu and we will see a wide range of step-by-step methods (shortcuts can be freely set according to the scheme and configuration)
Step:One step forward. If the breakpoint contains sub-methods, the method will be entered (the method of the official class library will not be entered );
Force Step:Any method can be entered Based on Step;
Smart Step:The statement contains two or more method chain calls. You can select the method to enter, including the anonymous internal class, or the Nimda expression;
Step Out:One step forward. If it is already in the sub-method, it jumps out;
Step Over:Move forward in one step without entering the sub-method;
Drop Frame:After clicking this button, you will return to the call of the current method for re-execution, and the value of all context variables will return to that time. As long as there are upper-level methods in the call chain, you can jump to any of them;
Resume Program: Continue execution;
Force Run to Cursor:As the name suggests, it is to jump directly to the cursor position, which is very convenient for debugging the required statement;
Now let's talk about the breakpoint;
Conditional breakpoint: Set the condition at the position of the breakpoint, and stop only when the condition is met. for example, when debugging A for loop, there are hundreds of loops, but it is quite convenient to see a certain number of times;
Method breakpoint: Set at the beginning of the method. The four black dots in red are marked at the breakpoint;
Log breakpoint: Right-click the breakpoint, remove Suspend, enter the expression to be printed in log evaluated expression: "setValue =" + getValue (), and then run the program;
Query
We can see the breakpoint log in the console:
After selecting log message to console, the log information is printed.
Exception breakpoint: Breakpoint triggered when an exception occurs: 1. Click breakpoint management and add an exception breakpoint.
If you set a method that can generate an exception and execute it, you will find that the exception will be stopped here;
If you set a method that can generate an exception and execute it, you will find that the exception will be stopped here;
Temporary breakpoint: After the breakpoint is stopped, it will be removed. During the debugging process, it will only be broken once. Press the ATL + left mouse button.
Invalid breakpoint: Temporarily invalidates the breakpoint, so you do not need to delete it. Method: ALT + left mouse click on the breakpoint
Exception breakpoint: Breakpoint triggered when an exception occurs: 1. Click breakpoint management, and add an exception breakpoint to set a method that can generate an exception and execute it. The exception will be suspended here;
If you set a method that can generate an exception and execute it, you will find that the exception will be stopped here;
Observe debugging:
When a breakpoint hits, we certainly need to observe and obtain the results.
Change variable value: For example, the original value of the execution method:
In the Variables window, right-click and modify to change the value:
Add to observation window: If multiple scalar or multiple expressions are in different places, it is very troublesome to observe them up and down. We can Add it to Watchs, then, you can view multiple variable values at a time in the observation window;
Calculation Expression: Calculate the value of this variable. This is the case when the result is run to the center to view the variable value.
Calculation method call result: Enter the method getValue () and click execute to obtain the result of this method;
Code fragment mode: run a piece of code to get the result, instead of an expression:
For example, we modify the segment of the variable value:
Mark object; Right-click the variable and choose Mark Object from the shortcut menu. Then, enter the tag:
At this point, the general content is complete. The debugging skills are generally breakpoint settings and step-by-step methods. I hope you can practice them more and write me or leave a message if you have any questions. Finally, the foundation is very simple, but it is very important. Do not look down on it. Every step can go further.
WelcomeLong pressFollow my public account on the QR code: