简体   繁体   English

Android打印堆栈跟踪

[英]Android print stack trace

How can I get the equivalent of a printStackTrace in android? 我怎样才能在android中获得相当于printStackTrace的东西? I know I can log an error, by passing in a tag name and a String to the logging method, but that just gives me a nullpointerexception. 我知道我可以通过将标记名称和字符串传递给日志记录方法来记录错误,但这只是给了我一个nullpointerexception。 If I call e.printStackTrace(), where is this data printed to? 如果我调用e.printStackTrace(),这个数据打印到哪里?

Log.e("mytag", "mymessage",ex);

If you are using eclipse make sure you have LogCat window opended. 如果您正在使用eclipse,请确保已打开LogCat窗口。 You can find it under 你可以找到它

Window-> Show View-> Other -> Under Android -> LogCat 窗口 - >显示视图 - >其他 - >在Android下 - > LogCat

updated:2014 July 更新时间:2014年7月

OR if you are using Android Studio you can find it under 或者,如果您使用的是Android Studio,则可以在下面找到它

Window menu -> Show view -> Logcat 窗口菜单 - >显示视图 - > Logcat

LogCat is like a console in normal JAVA. LogCat就像普通JAVA中的控制台一样。 Your e.printstacktrace() would appear in LogCat as well. 您的e.printstacktrace()也会出现在LogCat中。

The data is still printed to the DDMS logs with e.printStackTrace(); 数据仍然使用e.printStackTrace()打印到DDMS日志; You can also use Log.e("Tag", "Description", e); 您也可以使用Log.e(“Tag”,“Description”,e); which will print the logs as well. 这也将打印日志。 DDMS is located under android-sdk/tools/ddms and logs will be shown in the bottom pane on launch. DDMS位于android-sdk / tools / ddms下,日志将在启动时显示在底部窗格中。

Firstly, in case you already haven't, you should be wrapping your code in a try-catch structure, like this, for example: 首先,如果你还没有,你应该将代码包装在try-catch结构中,例如:

private String TAG = "MyMethodName";  // change this to anything that makes sense to you       
try {
    ...
    Log.d(TAG, "processed OK");
} catch (Exception except) {
    Log.e(TAG,"CRASH StackTrace: "+ Arrays.toString(except.getStackTrace()));
}

This pseudocode will show a complete stack trace. 此伪代码将显示完整的堆栈跟踪。

NEWBIE ALERT: Before publishing app, don't forget to "comment out" (using // ) this and all debugging features that may be used to expose the internals of your code. NEWBIE ALERT:在发布应用程序之前,不要忘记“注释掉”(使用// )这个以及可能用于公开代码内部的所有调试功能。 Anyone who plugs a device running your code with ADB enabled will be able to access LogCat (where Log publishes). 任何在启用ADB的情况下插入运行代码的设备的人都可以访问LogCat( Log发布的地方)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM