Android print stack trace

2020-06-16 02:59发布

How can I get the equivalent of a printStackTrace in android? 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. If I call e.printStackTrace(), where is this data printed to?

标签: android
4条回答
Melony?
2楼-- · 2020-06-16 03:26

If you are using eclipse make sure you have LogCat window opended. You can find it under

Window-> Show View-> Other -> Under Android -> LogCat

updated:2014 July

OR if you are using Android Studio you can find it under

Window menu -> Show view -> Logcat

LogCat is like a console in normal JAVA. Your e.printstacktrace() would appear in LogCat as well.

查看更多
放我归山
3楼-- · 2020-06-16 03:27
Log.e("mytag", "mymessage",ex);
查看更多
够拽才男人
4楼-- · 2020-06-16 03:28

Firstly, in case you already haven't, you should be wrapping your code in a try-catch structure, like this, for example:

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. Anyone who plugs a device running your code with ADB enabled will be able to access LogCat (where Log publishes).

查看更多
贪生不怕死
5楼-- · 2020-06-16 03:40

The data is still printed to the DDMS logs with e.printStackTrace(); You can also use 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.

查看更多
登录 后发表回答