No matter what kind of program development process, errors are unavoidable, in general, grammatical errors will be detected by the development environment, and prompt us to the wrong location and the method of modification, but the logic error is not so easy to find, usually the location and analysis of logic errors is a very difficult thing.
So in the course of learning Android, after learning its program structure, writing HelloWorld and some simple interface layout program, I chose to study its debugging tools. Logcat is one of them. Logcat is a tool used to obtain system log information, which can capture information including Dalvik virtual machine generated information, process information, activitymanager information, Packagermanager information, Android runtime information and application information, and so on.
After opening eclipse, we can select the window–> Show View->other menu and select Logcat in Android->logcat, so Logcat will appear in the lower area of Eclipse.
Among them, the 5 letters in the upper right of Logcat represent 5 different types of log information (and are distinguished by different colors, the higher the level, the more prominent the color):
1. [V]: detailed (Verbose) information, the output color is black
2. [D]: Debug (Debug) information, output color is blue
3. [I]: Notice (info) information, output color is green
4. [W]: Warning (Warn) information, output color is orange
5. [E]: Error information, the output color is red, here the error message is the highest level, followed by a warning message, followed by notification information and debugging information, the lowest level is the details.
6.[assert], new version added.
In Logcat, we can advertise these 5-letter icons to select the type of information to display, and information that is higher than the selected type is displayed in the Logcat, but information that is below the selected type is not displayed.
In the Android program debugging process, the first to introduce the Android.util.Log package, and then use to log the details of the LOG.V (), used to record debug information log.d (), used to record the notification information LOG.I (), used to record the warning message LOG.W () , LOG.E (), which is used to log error messages, five functions set "log Points" in the program. The first parameter of these functions is the log tag tag (that is, you need a constant to mark the location you want to test, the name of the tag is the so-called log tag), and the second parameter is the actual information content. Whenever the program runs to a "log point" that we set, the application's log information is sent to Logcat, and we can determine whether the program has errors based on the information displayed by the "Log Point" and the information content that we expect to set in the function, so that we can quickly find the wrong "log point" The location of the accident. "
In the following program, the specific use of the log class is demonstrated:
1 Packagewt.relativelayout;2 5 Importandroid.app.Activity;6 7 ImportAndroid.os.Bundle;8 9 ImportAndroid.util.Log;Ten One ImportAndroid.widget.Button; A - ImportAndroid.widget.EditText; - the ImportAndroid.widget.TextView; - - + Public classRelativelayoutextendsActivity { A at Final StaticString TAG = "LOGCAT"; - - PrivateTextView TV =NULL; - - PrivateEditText et =NULL; - in PrivateButton BTN1 =NULL; - to PrivateButton btn2 =NULL; + - @Override the * Public voidonCreate (Bundle savedinstancestate) { $ Panax Notoginseng Super. OnCreate (savedinstancestate); - the Setcontentview (r.layout.main); + ATV =(TextView) Findviewbyid (R.id.label); the +ET =(EditText) Findviewbyid (r.id.entry); - $BTN1 =(Button) Findviewbyid (r.id.cancel); $ -BTN2 =(Button) Findviewbyid (R.id.ok); - the Tv.settext (r.string.name); - Wuyi Btn1.settext (r.string.button1); the - Btn2.settext (r.string.button2); Wu - About $LOG.V (TAG, "This is Verbose"); - -LOG.D (TAG, "This is Debug"); - ALOG.I (TAG, "This is Info"); + theLOG.W (TAG, "This is Warn"); - $LOG.E (TAG, "This is the Error"); the the } the the}
After the program runs, Logcat captures the log information sent by the application, showing the results such as:
Through the results we found that even if we have selected a particular level of log information, the resulting log information is still a lot, we need to read it, to bring us a lot of trouble. At this point we will use a logcat to provide the "filter" function, in the upper right corner of the "+" and "-", respectively, is to add and delete filters. We can filter the displayed log content based on the tag of the log information, the process number (PID) that generated the log, or the level of information. In practice, we'd better declare a string constant tag for each class so that we can easily distinguish the logs of different classes in Logcat.
Use the "+" sign in the upper-right corner of the LOGCAT, add a filter named Logcatfilter, and set the filter to "label =logcat" for the specific setup method of the filter:
After the filter is set up, logcatfilter the filtered log information such that, regardless of the type of log information, which process belongs to, as long as the label is Logcat, will be displayed in the Logcatfilter.
After running the test, the log in Logcat is filtered by configuring the L filter
In this way, the commissioning work is complete.
The Android Program debug Logcat (RPM)