Android print stack trace

前端 未结 4 2310
忘掉有多难
忘掉有多难 2021-02-15 12:36

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 nu

4条回答
  •  离开以前
    2021-02-15 13:02

    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).

提交回复
热议问题