四、在ANDROID中调试程序
1、ANDROID中日志信息分为四种:错误(ERROR)、警告(WARN)、通知(INFO)、调试(DEBUG)、详细(VERBOSE).除了在开发阶段,VERBOSE从不被编译到一个应用程序中,调试日志被编译但在运行时会跳过,Error, warning 和 info 日志总是 存在。
2、android.util.Log类源码:
package android.util; import com.android.internal.os.RuntimeInit; import java.io.PrintWriter; import java.io.StringWriter; public final class Log { /** * Priority constant for the println method; use Log.v. */ public static final int VERBOSE = 2; /** * Priority constant for the println method; use Log.d. */ public static final int DEBUG = 3; /** * Priority constant for the println method; use Log.i. */ public static final int INFO = 4; /** * Priority constant for the println method; use Log.w. */ public static final int WARN = 5; /** * Priority constant for the println method; use Log.e. */ public static final int ERROR = 6; /** * Priority constant for the println method. */ public static final int ASSERT = 7; private Log() { } /** * Send a {@link #VERBOSE} log message. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. */ //打印详细信息 public static int v(String tag, String msg) { return println(VERBOSE, tag, msg); } /** * Send a {@link #VERBOSE} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. * @param tr An exception to log */ public static int v(String tag, String msg, Throwable tr) { return println(VERBOSE, tag, msg + '/n' + getStackTraceString(tr)); } /** * Send a {@link #DEBUG} log message. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. */ //打印调试信息 public static int d(String tag, String msg) { return println(DEBUG, tag, msg); } /** * Send a {@link #DEBUG} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. * @param tr An exception to log */ public static int d(String tag, String msg, Throwable tr) { return println(DEBUG, tag, msg + '/n' + getStackTraceString(tr)); } /** * Send an {@link #INFO} log message. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. */ //打印通知信息 public static int i(String tag, String msg) { return println(INFO, tag, msg); } /** * Send a {@link #INFO} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. * @param tr An exception to log */ public static int i(String tag, String msg, Throwable tr) { return println(INFO, tag, msg + '/n' + getStackTraceString(tr)); } /** * Send a {@link #WARN} log message. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. */ //打印警告信息 public static int w(String tag, String msg) { return println(WARN, tag, msg); } /** * Send a {@link #WARN} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. * @param tr An exception to log */ public static int w(String tag, String msg, Throwable tr) { return println(WARN, tag, msg + '/n' + getStackTraceString(tr)); } /** * Checks to see whether or not a log for the specified tag is loggable at the specified level. * * The default level of any tag is set to INFO. This means that any level above and including * INFO will be logged. Before you make any calls to a logging method you should check to see * if your tag should be logged. You can change the default level by setting a system property: * 'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>' * Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPRESS will * turn off all logging for your tag. You can also create a local.prop file that with the * following in it: * 'log.tag.<YOUR_LOG_TAG>=<LEVEL>' * and place that in /data/local.prop. * * @param tag The tag to check. * @param level The level to check. * @return Whether or not that this is allowed to be logged. * @throws IllegalArgumentException is thrown if the tag.length() > 23. */ public static native boolean isLoggable(String tag, int level); /* * Send a {@link #WARN} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param tr An exception to log */ public static int w(String tag, Throwable tr) { return println(WARN, tag, getStackTraceString(tr)); } /** * Send an {@link #ERROR} log message. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. */ //打印错误信息 public static int e(String tag, String msg) { return println(ERROR, tag, msg); } /** * Send a {@link #ERROR} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. * @param tr An exception to log */ public static int e(String tag, String msg, Throwable tr) { int r = println(ERROR, tag, msg + '/n' + getStackTraceString(tr)); RuntimeInit.reportException(tag, tr, false); // asynchronous return r; } /** * Handy function to get a loggable stack trace from a Throwable * @param tr An exception to log */ public static String getStackTraceString(Throwable tr) { if (tr == null) { return ""; } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); tr.printStackTrace(pw); return sw.toString(); } /** * Low-level logging call. * @param priority The priority/type of this log message * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param msg The message you would like logged. * @return The number of bytes written. */ public static native int println(int priority, String tag, String msg); }
3、Log类使用方法
在类中定义一个标签:
private static final String TAG = "MyActivity";
然后可以调用Log类的静态方法:
Log.代号(标签,信息)
例如:Log.v(TAG, "信息" );
然而在实际应用中,我们可以在处理输入错误的“try...catch”语句中加入Log信息,好让我们从记录数据中,追踪到输入错误的情况。
public class Bmi extends Activity{ private static final String TAG = "Bmi"; ...... catch(Exception err){ Log.e(TAG,"error:"+err.toString() ); } }
4、在调试模式下运行虚拟机。
1>启动虚拟机(菜单栏 > Run > Debug History > 程序名)
2>在Eclipse下点击右上角的"Open Perspective" ---> Other选项 ---> Debug
5、LogCat窗口的使用
点击绿色的“+”号,添加一个过滤器,弹出下面窗口:
Filter Name:填入过滤器名字(自定义)
by Log Tag:填入标签的名字
其它的不用填。
也可以使用命令来查看所有的信息记录:adb logcat --help
6、虚拟机上的查错设置
主界面 ---> DevTools ---> Developments Settings ---> 在Show CPU Usage打钩 或者 在Showrunning process打钩
详细的就不说明了!
更多相关文章
- android配置信息类-Configuration
- android 获取屏幕尺寸,密度等信息
- Android错误:java.lang.RuntimeException: Unable to start activ
- android中调用inflate时出现的警告错误处理
- Android 编译错误解决----1
- Android接收信息操作
- android获取版本信息、屏幕信息和设备编号