e.printStackTrace(); in string

前端 未结 5 1402
执笔经年
执笔经年 2021-01-30 13:00

There are e.printStackTrace() method to print exceptional error, so I would like to take entire exception in String and show it by Toast.makeText

5条回答
  •  忘掉有多难
    2021-01-30 13:36

    In your exception handler use:

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    
    e.printStackTrace(pw);
    
    whateverFunctionYouLikeToPrintYourStackTrace(sw.getBuffer().toString());
    

    However, you're much better off using ADB with logcat, because stack traces on Toasts look terrible.

提交回复
热议问题