Android Studio Code Debugging Daquan

Source: Internet
Author: User

http://blog.csdn.net/dd864140130/article/details/51560664

Android Studio has become the main tool to develop Android, and it is quite handy to be familiar with it. As a developer, debug and find bugs to solve, but our housekeeping skills. Just so-called, 工欲善其事 its prerequisite, like the other development tools, such as Eclipse, Idea,android Studio also provides us with powerful debugging techniques, and today we'll look at the debugging techniques in Android Studio. First, take a look at the debug panel provided for us in Android Studio (standard case): Here's a picture description click Restore ' Threads ' view in the top right corner to show the currently relevant thread information: Write a picture here about Android Studio provides us with 7 functional areas: Single-step debug zone Breakpoint Management Area evaluation expression thread frame stack object variable area variable Observation area below we describe each of the seven areas. One-step debugging area This area provides the main debugging operations, as you know, mainly: Step over, step into, force Step into, step out, drop frame. Show execution point here to write a picture description click the button and the cursor will navigate to the location you are currently debugging. Step over here to write a picture description to skip over, click on this button will cause the program to execute a line down. If the current row is a method call, the method called by this line is executed and then to the next line. For example, the current code is:intnum=10;intMin=math.min (num,100); System.out.println (min);

If the second row is currently being debugged, when you click Step Over, Math.min (num,100) method to skip to the third line after executing the first step. Step into here to write a picture describing a single-step jump, performing this action will cause the program to execute a line down. If the row has a custom method, proceed inside the method and be aware that if it is a method in the class library, it will not go inside the method. Forces step into here to write a picture description force stepping in, similar to the step into function, the main difference is: If the current line has any method, whether the method is our own definition or the class library provided, can jump into the inside of the method to continue to execute the drop Frame here writes a picture describing a name that is not well remembered, understood as an interrupt execution, and returns to the initial point of execution of the method, where the corresponding stack frame of the method is removed from the stack. In other words, if the method is called, it is returned to the current method being invoked, And the value of all context variables is also restored to the state when the method was not executed. A simple example: Public classDebugdemo {PrivateString name = "Default"; Public voidAlertname () {System.out.println (name); Debug (); } Public voidDebug () { This. Name = "Debug"; } Public Static voidMain (string[] args) {NewDebugdemo (). Alertname (); }}When you debug Debug (), the operation is performed, and the callback is called to Debug (), which is the Alertname () method. If you continue execution of the drop frame at this point, the callback will be to the place where Alertname () is called, that is, main (). Force Run to cursor here write a picture describing a very useful feature that can ignore existing breakpoints and jump to where the cursor is located. For a simple example: here is a description of the 10th line, at this time I want to debug 18 lines and do not want to step by step debugging, can I put it in place? ?we just need to position the cursor to the appropriate location and then execute the force Run to cursor: Here's a picture description evaluate expression here to write a picture description Click this button to embed an interactive interpreter at the current debug statement, in which the interpreter You can perform an evaluation operation on any expression that you want to perform. For example, when we debug, we execute the following code: In this case, the evaluate Expression is executed at this time, which is equivalent to embedding an interactive interpreter before the trial, so what can we do in the interpreter? Here, we can evaluate the result: you want to ask for the right mouse button, select Evaluate expression. This will be displayed as follows: Here is a picture description in the pop-up input box to enter the evaluation expression, for example, here we enter Math.min ( result,50), as described here to write a picture describing click execution, we found that the results have been output in result, as follows: Here is a picture Description breakpoint Management zone return here to write a picture description Click the button will stop the current application, and restart. In other words, you can use this action when you want to re-debug , well, that's the meaning of a re-visit. Pause program here to write a picture description Click this button to pause the execution of the app. If you want to recover, you can use the following resume Program.resume program here to write a picture describing the operation has the meaning of recovery application, but there are two kinds of behavior:1when the app is paused, tapping the button will restore the app to run.2In many cases, we will set multiple breakpoints for debugging. In some cases, we need to move from the current breakpoint to the next breakpoint, the code between the two breakpoints is automatically executed, so that we do not need to step-by-step debugging to the next breakpoint, save time and effort. To illustrate: Public voidTest () {test1 (); ... test2 ();}Let's say we add breakpoints in lines 2nd and 4th, respectively. If we debug at the 2nd line at this point, click to do this, the current debug location will be automatically executed to line 4th, that is, the 2nd to 4th line between the code will be automatically executed. Stop here to write a picture description Click this button to terminate the current process through the associated closing script. In other words, there may be different stopping behavior for different types of projects, such as: For normal Java projects, clicking on this button means exiting debug mode, but the app will also perform. In Android projects, Clicking this button means that the app ends up running. Here we take a common Java project as an example: here to write a picture description at this time if we do stop operation, found that the program exited debug mode, and normal execution, the console results are as follows: here to write a picture description view Breakpoints here to write a picture description Click the button will go to the breakpoint management interface, where you can view all breakpoints, manage or configure the behavior of breakpoints, such as: Delete, modify property information, etc.: here to write a picture description mute Breakpoints here to write a picture description use this button to toggle the state of the breakpoint: Start or disable. During debugging, you can disable all breakpoints temporarily disabled, and the application will run normally. This is useful, for example, when you suddenly don't want a breakpoint to interfere with the process you're concerned with during debugging. Breakpoints can be temporarily disabled. Get thread dump here to write a picture description gets the threads dump, click on the button will go into the thread dump interface: here to write a picture description Let's introduce the dump interface: the most common thread tool area is to write a picture description, which can be used to filter threads, The others do not explain the parsing come on, let's get to know the type of thread, which is represented as a different icon: Thread Status Description icon thread is suspended. Here is a picture describing thread is waiting on a monitor lock. Write a picture here describing thread is running. Here is a picture description thread is executing network operation, and is waiting fordata to is passed. Here is a picture describing thread is idle. Write a picture here describing the event Dispatch Thread, which is busy. Here is a picture describing thread is executing disk operation. Here to write the picture description settings here to write a picture description Click the button to open a list of settings: Here is a description of the picture we have to explain: show values inline debugging to turn on the function, will be the code to the right of the value of the variable, the red box is shown in the section: Here is a picture description Show method return values the function is enabled during debugging, and the return value of the last execution method is displayed in the variable area. For example, first of all, to turn off this function, we debug this code and observe its variable area: here, write a picture describing the function, To observe the variation of the variable area: Here is the description of the picture to continue debugging: here to write the picture description continue to debug: here to write a picture description of this function is simply great, debugging a piece of code, and want to see the final call method in the code of the end result is very useful. Auto-Variables mode opens this function, the idea's debugger will automatically evaluate certain variables, presumably when you execute at a breakpoint, debugger detects the state of the variable before or after the current tuning, and then selectively outputs it in the variable area. For example, Before the feature is turned on, the variable area outputs all the variable information: When you debug to line 13th, debugger detects that the NUM variable is not being used after it is opened, the variable will not be output in the variables section. Here's a picture describing the sort values alphabetically turn on this feature, the output in the variable area is sorted alphabetically, very simple, not used, or in the default order. Help here to write a picture description of this needless to say, there is no understanding of any can view the official documentation, which is one of the best documents I have ever seen. Several other operations: Settings,pin,close for you to use. Modify the value of a variable during debugging, we can easily modify the value of a variable, as follows: Here is the picture description in, the current result value is calculated as 10, here we pass the set Value modifies the result of its calculation to 100. Variable Observer area This area displays the value of the variable you are interested in. In debug mode, you can add a variable to observation area by adding to watches, and the change will be displayed in the variable observation area. The operation is as follows: Here is a picture description here we are interested in name, we would like to see its value change, so we will be "special care". Note that because name is a member variable, you can also see the value in the object's observer area. If it is a local variable, it can only be used in this way. Classification of breakpoints so far, we have simply introduced the debug Ribbon, Breakpoint Management area, evaluation expression, the function of these three areas. On top of that, we constantly mention the break point once, but what is the breakpoint? Presumably most people already know, we are here in a simple explanation: breakpoints are one of the debugger's functions, allowing the program to pause where it is needed to help us run the analysis process. In Android studio, breakpoints are also the following five classes: Conditional Breakpoint Log Breakpoint Exception Breakpoint Method Breakpoint Property breakpoint where method breakpoint is the type of breakpoint we are most familiar with, I believe no one will. We highlight the other four types of breakpoints below. Conditional breakpoints the so-called conditional breakpoint is the breakpoint that occurs on a particular condition, that is, we can set a breakpoint to be interested only in an event, the most typical application being in a list loop, we want to pause the program when a particular element appears. For example, now that we have a list that contains four elements of q,1q,2q,3q, we want to traverse to 2Q When the program is paused, you need to do the following: Add a breakpoint where needed, as follows: Write a picture here to describe the left-click at the breakpoint and fill in the filter conditions at condition. Here we only care about 2q, so fill in s.equals ( "2q"write a picture here describing a log breakpoint this type of breakpoint does not cause the program to stop, but instead outputs the log information that we want it to output and then proceeds to execute. Do the following: also left-click on the breakpoint and uncheck suspend in the popup dialog. Here to write the picture description in the pop-up Control Panel, select Log evaluated expression, and then fill in the log information you want to output, as follows: Write a picture here describes when the debugging process encounters the breakpoint will output the result, as follows: Write a picture here describe an exception breakpoint the so-called exception breakpoint is in the debugging process, in the event of an exception (you can specify a class of exceptions), it will immediately locate the exception throws the place. For example, in the debug exception, we are very concerned about the runtime exception, in the hope of generating any running exception in time to locate, then you can take advantage of this type of exception, before the launch, the exception breakpoint debugging is very helpful to reduce the chance of crash in the formal environment. Here's how: in the Run menu item, select View breakpoints (you can also click here to write a picture description in the Breakpoints Admin panel), as follows: Write a picture here in the Manage Breakpoints panel, click+here to write the picture description in the pop-up dropdown select list, we choose Java Exception Breakpoints here to write a picture description Here we select Search by Name, enter the exception type we care about in the input box below. Here we care about NullPointerException, and the debugger locates the exception where the nullpointerexception occurs once the debugging process has occurred. Here write the picture description method breakpoint Here Write the picture description (skip it, should no one do not know) Filed watchpoint here to write a picture description Filed Watchpoint is essentially a special breakpoint, also known as a property breakpoint: When a field value is modified, The program pauses at the point of modification. This is especially useful when debugging multiple threads, which can help us locate concurrency errors in a timely manner. Its use and add ordinary breakpoint and no different, breakpoint icon slightly different debugging two ways to the present, the relevant basis for debugging we have already introduced, but many children's shoes on the Android studio here to write the picture description of the two buttons are confused: Debug and attach process. Here we briefly introduce the difference between the two: debug: Install in debug mode, breakpoints can be set before running, or can be set after running, is the most common mode for most people attach process: Compared with the debug mode, The ability to attach the debugger to any running process. For example, we can pass the attach process to the processes that we want to debug. Then, set the relevant breakpoint where needed. In the specific commissioning process, you can choose your own discretion. Back, I'll take you step-by-step to debug Android

Android Studio Code Debugging Daquan

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.