作者:米五仔8 | 来源:互联网 | 2023-05-27 18:35
我正在尝试在我的设备上调试应用程序,我在使用调试器时遇到了一些麻烦.我尝试测试记录器,看它是否会像这样写入Logcat:
Log.d("MyActivity", "Testing logging...");
但是在Logcat中没有任何东西显示app: com.myapp.debug
过滤器.它出现时我只是按字符串过滤(使用我的应用程序名称),但条目如下所示:
01-08 13:45:07.468 29748-29748/? D/MyActivity? Testing logging...
这个问号是否意味着应用程序中的某些内容没有传递给调试器?这可能与我调试器的第二个问题有关:
我一直在调试崩溃,每次发生时,手机只会显示"应用程序没有响应"消息,然后关闭当前活动,断开调试器连接,应用程序继续运行上一个活动.没有堆栈跟踪,没有关于崩溃的信息,没有.我需要在Android Studio中设置一些东西以使其正常工作吗?
1> 小智..:
我也有这个麻烦,我找不到太好的答案.相反,我做了一个解决方法,并使用Thread.setDefaultUncaughtExceptionHandler()捕获错误并使用Log.e()记录它.
我用这个课来做.
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);
}
}
然后在我的活动中.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* catch unexpected error
*/
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
setContentView(R.layout.activity_main);
//other codes
}
希望这可以帮助.