java 日志记录和崩溃堆栈跟踪未显示在 Android Studio 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27841856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging and crash stack traces not showing in Android Studio
提问by benwad
I'm trying to debug an app on my device and I'm having a bit of trouble with the debugger. I tried testing the logger to see if it would write to Logcat like so:
我正在尝试调试设备上的应用程序,但调试器遇到了一些麻烦。我尝试测试记录器,看看它是否会像这样写入 Logcat:
Log.d("MyActivity", "Testing logging...");
But nothing shows up in Logcat with the app: com.myapp.debug
filter. It comes up when I simply filter by string (using my app name) but the entry looks like this:
但是 Logcat 中没有显示任何带有app: com.myapp.debug
过滤器的内容。当我简单地按字符串过滤(使用我的应用程序名称)时出现,但条目如下所示:
01-08 13:45:07.468 29748-29748/? D/MyActivity﹕ Testing logging...
Does this question mark mean that something in the app is not getting passed through to the debugger? This might relate to my second issue with the debugger:
这个问号是否意味着应用程序中的某些内容没有传递给调试器?这可能与我使用调试器的第二个问题有关:
I've been debugging a crash and every time it happens, the phone simply shows the 'App is not responding' message then closes the current activity, disconnects the debugger, and the app keeps on running with the previous activity. No stack trace, no info about the crash, nothing. Is there something I need to set up in Android Studio to get this working?
我一直在调试崩溃,每次发生崩溃时,手机只显示“应用程序没有响应”消息,然后关闭当前活动,断开调试器,应用程序继续运行前一个活动。没有堆栈跟踪,没有关于崩溃的信息,什么都没有。是否需要在 Android Studio 中进行设置才能使其正常工作?
回答by
I'm also having this trouble and I can't find too a good answer for this. Instead I did a work around and catch the error with Thread.setDefaultUncaughtExceptionHandler() and Log it with Log.e()
我也遇到了这个问题,我找不到很好的答案。相反,我做了一个解决方法并使用 Thread.setDefaultUncaughtExceptionHandler() 捕获错误并使用 Log.e() 记录它
I used this class to do it.
我用这个类来做到这一点。
public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {
private final String LINE_SEPARATOR = "\n";
public static final String LOG_TAG = ExceptionHandler.class.getSimpleName();
@SuppressWarnings("deprecation")
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
StringBuilder errorReport = new StringBuilder();
errorReport.append(stackTrace.toString());
Log.e(LOG_TAG, errorReport.toString());
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
Then in my Activity .
然后在我的 Activity 中。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* catch unexpected error
*/
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
setContentView(R.layout.activity_main);
//other codes
}
Hope this helps.
希望这可以帮助。
回答by QArea
I think it is the same adb or filer problem. At first remove all filters. Restart adb - type in terminal adb kill-server && adb start-server.
我认为这是相同的 adb 或 filer 问题。首先删除所有过滤器。重新启动 adb - 在终端输入 adb kill-server && adb start-server。
回答by Abdul Samad
Probably your google analytics "ga_reportUncaughtExceptions" is set to true, turning it to false fixes the issue and exceptions get printed to logcat.Please refer to below link for further details.
可能您的 google 分析“ga_reportUncaughtExceptions”设置为 true,将其设置为 false 可修复问题,并将异常打印到 logcat。有关详细信息,请参阅以下链接。
Why does android logcat not show the stack trace for a runtime exception?