Reprint------------------some tips on Android

Source: Internet
Author: User
Tags log log switch case

program Debugging in Android Eclipse

One: Breakpoint debugging

When you develop an Android program with Eclipse, you can use breakpoints as a single step in debugging.
The steps are as follows.
1 Set Breakpoints: Double-click on the left border of the encoded form, or right-click on the menu and select the Toggle Breakpoint menu item.



2 run the program in debug mode: Click the Bug button on the toolbar or right-click on the project and select the Debug as,android Application menu to start the debug mode of the program.
The first time you run debug mode eclipse will pop up the following confirmation window




When the program runs to your breakpoint, it will stop, then you can follow the following function keys to debug on demand:
[1] The shortcut key (F8) executes the program directly until it stops at the next breakpoint.
[2] shortcut key (F5) step to execute the program, encountered when the method entered.
[3] shortcut key (F6) step into the program, encountered when the method skipped.
[4] shortcut key (F7) step into the program, jump out of the current method.

The current value of the variable when viewing a breakpoint: Right-click on the corresponding variable, select the Watch menu item on the menu, and the value of the variable will appear in the Expressions window.




Second, the Android application debugging Tools

Eclipse Java Editor

This editor is not only able to provide developers with code writing, grammar correction and real-time compilation and other common functions, but also the Java source code can be quickly modified, refactoring and other advanced operations.

Details: The error prompt (a small light bulb icon and a red X) indicates where the error is, there is a red mark in the upper-right corner of the screen, and it is useful to prompt the developer that there is an error somewhere in the document. You can also see a red rectangle in the right margin of the document, which indicates the approximate location of the error, and if the code is very large, you can quickly locate the error by dragging the vertical scroll bar to the position of the red rectangle block.

Java and Dalvik compiling, building systems

The Android app eventually runs on the Dalvik virtual machine on the phone (or emulator). As a result, the Java compiler and the Dalvik bytecode converter are needed for compiling and building Android applications.

Eclipse Debugger

The Android SDK provides a mapping from Dalvik bytecode to Java source code so developers can debug Android applications directly using Eclipse's powerful debugger.

Details: The Android SDK is completely transparent to the Eclipse debugger and can be debugged in eclipse like a normal Java application. Setting breakpoints in your code is a common debugging tool that allows you to set breakpoints in eclipse in the following 3 ways.

    • Use the menu command. First place the cursor on the line where you want to set the breakpoint, and then execute the menu command Run->toggle breakpoint.
    • Use the keyboard. Select the line where you want to set the breakpoint, and press the shortcut key ctrl+shift+b on the keyboard.
    • In the editor, double-click the space you want to set to the left of the breakpoint line.

Execute the menu command run->debug->android application and start debugging the program. The initialization process is the same as the normal run program, if you need to rebuild the project, and then start the emulator to load the program. The Debugtest user interface will appear on the emulator after the program starts normally. The program will stop executing at the breakpoint location and eclipse will automatically switch to the debug layout. The following views are included in the debug layout:

    • Debug

The Debug tab page is used to display the call stack during program execution. On the Debug tab, there are some function buttons on the toolbar that provide the ability to continue, pause, terminate, step, process, and return.

    • Variables and breakpoints

The Variables tab can display all variable values within the scope of the current code. All breakpoints in the program are listed in the Breakpoints tab.

    • Editor

The editor in the debug layout is the same as the editor in the Java layout, except that the code currently executing in the debug layout is highlighted.

    • Outline

The outline view can show the current project's structure chart.

    • Console/tasks/properties

These three views are located in the lower-left corner of the debug layout, where the console (command line) view is the most useful and many important information is displayed in the console view during program debugging.

Logcat

Logcat is a common logging tool in the Android SDK. You can print status information and error messages through Logcat while the program is running. Logcat Another important use is to report progress to the developer during program startup and initialization.

When the application is loaded and started in the emulator, Eclipse automatically switches to the debug layout, and various information about the state of the program's operation appears in the Logcat view at the bottom right. For more convenient browsing of content in the Logcat view, you can click the Maximize button in the upper-right corner of the Logcat view with your mouse. The information that appears in the Logcat view, in the order in which the messages are generated, starts with a message about the start of the emulator, followed by the Android operating system startup message, then the various application startup messages, and finally the message associated with the loader launch. A few buttons labeled V, D, I, W, and e are visible in the toolbar of the Logcat view, and their role is to filter the messages.

    • V (Verbose) displays all types of messages;
    • D (Debug) displays debug, information, warning, and error messages;
    • I (information) displays only information, warning, and error messages;
    • W (Warning) displays only Warning and error messages;
    • E (Error) displays only the error message.

The following columns are included in the Logcat view:

    • Time: Used to show when the message was generated;
    • Priority (this column does not show a landmark in the title bar) the level of messages (values are D, I, W, or E, respectively, for debug, information, warning, and error);
    • PID: The process ID that generated the message;
    • Tag: A short description of the source of the message;
    • Message: Detailed content of the messages.

Note: In the process of program development, if more than one person needs to collaborate to debug incorrectly, the Logcat log should be shared.  The way to export Logcat logs is very simple, first select the log content you want to export in the Logcat view, and then click the down arrow in the upper-right corner of the Logcat view, and a menu pops up, at the bottom of the menu, with a name exports Selection as Text menu item, execute this menu item, you can save the selected log as a text file.

Android Debug Bridge (ADB)

ADB is a command-line tool that is included in the Android SDK to operate and debug directly on an Android phone or simulator.

DDMS (Dalvik virtual Machine Debug Monitoring service, Dalvik Debug Monitor Services)

DDMS is a windowed program debugging environment included in the Android SDK that closely integrates with Eclipse and Dalvik virtual machines.

Read MORE: See the log-common ADB commands.

Traceview

An Android platform-specific debugging tool that tracks and reports the time-consuming of all method calls and each method during the program's run.

Three, Android debugging summary

One. Log log output

Log Cat interface can be paged out at Window->show View->other->android->log Cat

Common log: Normal operation information: I error message: E

Output log: LOG.I (TAG, strings); Where tag is the log identifier, typically expressed as a class name (easy to see if this log is the output of a class), and is often declared as a static constant. Stirngs is the string to be output. Example:

       log.i (TAG, strings)

After running the program successfully, you can view the information identified as phonesmstest in the output log log Cat view.

PS: You can create a filter in log cat, log cat->create filter->filter Name: Feel free. By tab Name: Log identifier, here is Phonesmstest

  Two Debug debugging

Double-click on the left side of the code Editor to set a breakpoint click on the menu bar Run (or F11) to start the program debugging. A dialog box pops up when the program runs to the breakpoint, clicks Yes to jump into the debug view, and finds the class being debugged

    

    

Run->step into statement (or F5)

Run->step over process (or F6, skip method)

Run->step return one step back (or F7, stepping into the method and jumping out.) );

Run->run to line run to cursor (or Ctrl + R)

Run->resume Intermittent run to end (or F8)

  Three. Unit Testing

1. Configure Androidmainfest.xml

Join in the application

<uses-library android:name= "Android.test.runner"/> <!--do not write to activity--

Join outside the application

    The value of the package is the same. -
<instrumentation android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "com. Phonesms.melody "android:label=" Test for my App "/>

2. Writing Unit test Code

       Note: In the Targetpackage package, you build the unit test class such as: in COM. Establish Phonesmstest.java in Phonesms.melody. This class must inherit the Androidtestcase and the test method must begin with testing, such as: Testphonesms ();

       

The code is as follows:

         Phonesmstest.java

In outline view (Outline), find the test method, right-->run as->android JUnit test. PS: Bring up outline:window->show View->outline

       

    

Iv. development of the Android Eclipse Debug Debugging detailed

1. Add a breakpoint to the program

If you add a program breakpoint in Eclipse, as shown:

  

There are three ways to add breakpoints in eclipse

The first type: Click on the first item in the red box after right-clicking on the Toggle breakpoint will add a program breakpoint on which line of your right-click code (the same action can cancel the program breakpoint)

The second: double-clicking the left mouse button in the Red box will add a program breakpoint on which line of the code you double-clicked (the same action can cancel the program breakpoint)

  

The third type: Use the shortcut key where the cursor is resting Ctrl + Shift + B will add a program breakpoint on the line where the cursor is resting (the same action can cancel the program breakpoint)

  

 2. Run debug debug to leave the program on the added breakpoint

Click on the drop-down menu in the Red box to select the item you want to debug and start Debug debugging, as shown in

If you do not select Direct click on the drop-down list to indicate debug run default project (default project is last Run project)

Debug debugging shortcut key for click F11

  

Analyze how to add a program breakpoint scientifically, in order to add a breakpoint to view the generated random number of values I have added 6 program breakpoints, green box represents the most scientific breakpoint location, red box indicates unscientific position. Let's analyze why, if the code fragment in the switch case is too long or the number of cases is too high if you add a program breakpoint in a red box, the programmer needs to add a lot of program breakpoints in case there is an omission so it will be difficult to quickly locate code execution there, if you use a green box to add a program breakpoint, Programmers just need to skip this line of code by pressing F6 at the breakpoint and go into the correct case to continue debugging.

  

Debug debugging Run, the program stopped in the Red box, press F6 to step through the discovery of a random number of 4 programs stuck in the green box, the programmer can quickly locate the random value of 4

3. Check the value of the variable after the program stops

The contents of the blue box are represented as the entry method of the breakpoint, just like your breakpoint is coming in from that method, learning to see this is really very important, as I now know clearly that one of my methods in the method will be called when the error occurs, but this method in the program 100 places are called, I might conclude that there was an error at the time of the call, I could not add a breakpoint to the 100 places where it was called, and I could quickly locate the problem by adding a program breakpoint to the method and then viewing the program from that place in the basket.

The green box allows you to see the values of all the variables in the current method, but if the variables are much more cumbersome to see here, you can use the red box method to view them.

In the red box, you can see the value of the variable by right-clicking the variable name in the expressions in the Purple box after you click Watch in the Coffee box.

How many program breakpoints have been added to the program in breakpoints.

  

4 share some tips for debugging in eclipse

Watch variables and our own program breakpoints are not automatically deleted by Eclipse unless we delete them manually or they will remain in the purple box, which slows down the Eclipse development tool, and if too much of it is likely to cause eclipse Crashes (there may be eclipse bugs), so that development becomes very painful, so the rain pine Momo here is recommended in each debug debugging in the Purple box before adding the program breakpoint and watch the variables are not manually emptied, only add this time to debug the required breakpoint on it, so Eclipse will not be crushed by these slow-down processes.

5. Connect the real-machine debugging

The first step is to open your phone in Settings select the application and then select Develop and then select USB Debugging.

The second step with the USB cable to connect the phone to the computer, the general situation will automatically install the driver, if you can not install the driver to download a pea pod or 91 assistant, let it help our phone automatically install the driver is very convenient.

The third step driver installs successfully in Device and sees the real machine (in red box) in the green box for the Android computer simulator

.

After running the project pop-up device Selection window The first one for the simulator in the second red box for me to connect to the computer's real machine Moto Milestone, after selecting the Click OK can be the real machine to debug the program, simple it? is not very to force, hehe.

  

Printing of log information in 6.Android development

I have done J2ME developed the development of Android iphone development found J2ME simulator and iphone simulator are very fast (simulator faster than the real machine) only Android simulator is the least force (real machine faster than the simulator) is really slow not to connect The real machine can be faster, but the same or slow, especially when the debug is too not to give a force (to complain that everyone do not mind oh. So sometimes when I'm developing Android, I don't have to go to debug. I'll use log to print the data I need. Below I teach you how to print log information under Andoid. I hope we all learn to use log.

The usual Log has 5: log.v () log.d () log.i () LOG.W () log.e ().

According to the first letter corresponds to Verbose,debug,info, Warn,error.

These log systems will print out.

Open logcat page Discovery system print a lot of log information we are not good to locate the log we just hit, click the "+" symbol in the red box pop-up below the window in the filter Name: And by Log tag: (blue box) fill in the log tag we just hit " Random "Note that both items must be filled in and then click OK to see the log of the random we just played in the green box. How is it still very simple huh huh.

  

Finally, it is useful to attach the shortcut keys that Eclipse uses in development. OH (reprint)

Ctrl+1 Quick Fix (the most classic shortcuts, you don't have to say more)

Ctrl+d: Delete When moving forward

Ctrl+alt+↓ Copy the current row to the next row (replication incremented)

Ctrl+alt+↑ Copy the current line to the previous row (replication incremented)

Alt+↓ the current line and the next line of interaction (especially useful, can be omitted first cut, then pasted)

Alt+↑ the current line and the upper row interaction position (IBID.)

alt+← the previous edited page

alt+→ next Edit page (of course, for the above article)

Alt+enter Displays the properties of the currently selected resource (project, or file or file)

Shift+enter inserts a blank row on the next line of the current row (at which point the mouse can be at any position in the current row, not necessarily the last)

Shift+ctrl+enter Insert blank line in current line (principle above)

Ctrl+q positioning to the last edited place

Ctrl+l positioning in a row (for people with programs over 100 have the gospel)

Ctrl+m maximize the current edit or view (and vice versa)

ctrl+/comment on the current line, and then uncomment it

Ctrl+o Quick Display OutLine

Ctrl+t quickly displays the inheritance structure of the current class

Ctrl+w Closing the current editer

Ctrl+k the selected word quickly navigates to the next

Ctrl+e quick display of the current Editer drop-down list (in bold if the current page does not appear)

ctrl+/(numpad) collapses all code in the current class

Ctrl+x (keypad) expands all code in the current class

Ctrl+space Code Helper to complete the insertion of some code (but the general and IME conflict, you can modify the input method hotkey, you can also take up alt+/to replace)

Ctrl+shift+e Display manager that manages all of the currently open view (you can choose to close, activate, and so on)

Ctrl+j forward Incremental lookup (after pressing CTRL+J, each letter editor you enter provides a quick match to navigate to a word, if not, it is not found in the Stutes line, check a word, especially useful, this function idea two years ago)

CTRL+SHIFT+J Reverse incremental lookup (same as above, except forward)

Ctrl+shift+f4 Close all open editer

Ctrl+shift+x the text that is currently selected is all stale lowercase

Ctrl+shift+y turns the currently selected text to lowercase

Ctrl+shift+f formatting of the current code

Ctrl+shift+p position to the match (for example {}) (after locating from the front, the cursor is in the match, back to front, or vice versa)

The following shortcut keys are commonly used in refactoring, I like and commonly used to tidy up (note: General refactoring of the shortcut keys are alt+shift beginning of the)

Alt+shift+r rename (is my own favorite, especially the variables and classes of rename, than the manual method can save a lot of labor)

Alt+shift+m Extraction Method (This is one of the most common methods of refactoring, especially useful for a whole bunch of mud codes)

Alt+shift+c Modify the function structure (more practical, there are n functions called this method, modified once done)

Alt+shift+l extract Local variables (you can directly extract some magical numbers and strings into a variable, especially when multiple calls are made)

Alt+shift+f change the local variable in class to a field variable (more useful function)

Alt+shift+i Merge variables (this may be a bit inappropriate inline)

Alt+shift+v moving functions and variables (not commonly used)

Alt+shift+z reconstruction of Regret medicine (Undo)

Edit

scope function Shortcut keys

Global Find and replace Ctrl+f

Text Editor Find previous Ctrl+shift+k

Text Editor Find Next Ctrl+k

Global Undo Ctrl + Z

Global Replication Ctrl + C

Global Restore Previous selection alt+shift+↓

Global clipping Ctrl+x

Global Quick Fix ctrl1+1

Global Content Assist alt+/

Global all check Ctrl + A

Delete delete globally

Global context information ALT +?

Alt+shift+?

Ctrl+shift+space

Java Editor display tooltip description F2

Java Editor Select encapsulated element alt+shift+↑

The Java editor selects the previous element alt+shift+←

The Java editor selects the next element alt+shift+→

Text Editor incremental find Ctrl+j

Text Editor incremental reverse lookup ctrl+shift+j

Global Paste Ctrl + V

Global Redo Ctrl+y

View

scope function Shortcut keys

Global magnification ctrl+=

Global Shrink ctrl+-

Window

scope function Shortcut keys

Global activation Editor F12

Global Switch Editor Ctrl+shift+w

Global on an editor Ctrl+shift+f6

Globally on a view CTRL+SHIFT+F7

Global on a Perspective view Ctrl+shift+f8

Global Next editor Ctrl+f6

Global Next View Ctrl+f7

Global Next Perspective Ctrl+f8

The text editor displays the ruler context menu Ctrl+w

Global Display View menu CTRL+F10

Global Display System Menu alt+-

Navigation

scope function Shortcut keys

Java Editor Open Structure ctrl+f3

Global Open Type Ctrl+shift+t

Global Open type Hierarchy F4

Global Open claims F3

Global Open External Javadoc shift+f2

Global Open Resource Ctrl+shift+r

Global fallback history alt+←

Global forward History alt+→

Global on a CTRL +,

Global Next Ctrl +.

The Java Editor displays outline Ctrl+o

Global open type Ctrl+shift+h in the hierarchy

Global go to matching parentheses ctrl+shift+p

Global go to previous edit location Ctrl+q

The Java editor goes to the previous member Ctrl+shift+↑

The Java editor goes to the next member Ctrl+shift+↓

Text editor go to line ctrl+l

Search

scope function Shortcut keys

Global presence in file Ctrl+shift+u

Global Open Search dialog box ctrl+h

Claims in the global workspace Ctrl+g

References in the global workspace Ctrl+shift+g

Text editing

scope function Shortcut keys

Text Editor overwrite toggle Insert

Text Editor Roll Line ctrl+↑

The text editor scrolls the line ctrl+↓

File

scope function Shortcut keys

Global Save Ctrl+x

Ctrl+s

Global Print Ctrl+p

Global Shutdown CTRL+F4

Global Save All Ctrl+shift+s

Global all off CTRL+SHIFT+F4

Global Properties Alt+enter

Global New CTRL + N

Project

scope function Shortcut keys

Global All Build Ctrl+b

Source

scope function Shortcut keys

Java Editor Formatting ctrl+shift+f

Java Editor Uncomment ctrl+\

Java Editor Comment ctrl+/

Java Editor Add import ctrl+shift+m

Java Editor Organization import Ctrl+shift+o

The Java Editor uses try/catch blocks to surround the settings, which are too common, so it is recommended to set them up here.

You can also use ctrl+1 to automatically fix it.

Run

scope function Shortcut keys

Global single-Step return F7

Global single-Step skip F6

Global single-step jump into F5

Global single-Step jump selection Ctrl+f5

Global Debug Last started F11

Global Continue F8

Global use filters to step SHIFT+F5

Global Add/Remove breakpoints Ctrl+shift+b

Global Display Ctrl+d

Global Run last boot ctrl+f11

Global Run to row ctrl+r

Global Execution Ctrl+u

Refactoring

scope function Shortcut keys

Global Undo Refactoring Alt+shift+z

Global Extraction Method Alt+shift+m

Global decimation local variable alt+shift+l

Global inline Alt+shift+i

Global Mobile Alt+shift+v

Global Rename Alt+shift+r

Global Redo Alt+shift+y

Reprint------------------some tips on Android

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.