debugging Android Studio中未显示日志记录和崩溃堆栈跟踪

wpcxdonn  于 2022-12-13  发布在  Android
关注(0)|答案(4)|浏览(416)

我正在调试我的设备上的一个应用程序,调试器有点问题。我试着测试记录器,看看它是否会像这样写入Logcat:

Log.d("MyActivity", "Testing logging...");

但是在Logcat中使用app: com.myapp.debug过滤器什么也没有显示。当我简单地通过字符串过滤时(使用我的应用程序名称),它出现了,但是条目看起来像这样:

01-08 13:45:07.468  29748-29748/? D/MyActivity﹕ Testing logging...

这个问号是否意味着应用程序中的某些内容没有传递到调试器?这可能与我使用调试器的第二个问题有关:
我一直在调试一个崩溃,每次发生时,手机只是显示“App is not responding”消息,然后关闭当前Activity,断开调试器,应用继续运行前一个Activity。没有堆栈跟踪,没有关于崩溃的信息,什么都没有。我需要在Android Studio中设置什么才能让它正常工作吗?

erhoui1w

erhoui1w1#

我也遇到了这个问题,我找不到一个很好的答案。相反,我做了一个工作,用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
   }

希望这对你有帮助。

ibps3vxo

ibps3vxo2#

我认为是adb或者过滤器的问题。首先删除所有过滤器。在终端中重新启动adb - type adb kill-server && adb start-server。

aemubtdh

aemubtdh3#

可能你的谷歌分析“ga_reportUncaughtExceptions”被设置为true,将其设置为false修复了这个问题,异常被打印到logcat。请参考下面的链接了解更多细节。
Why does android logcat not show the stack trace for a runtime exception?

dced5bon

dced5bon4#

您应该定义一个实现UncaughtExceptionHandler的类,并在Kotlin中使用stackTraceToString

import android.util.Log
import java.lang.Thread.UncaughtExceptionHandler

class ExceptionHandler : UncaughtExceptionHandler {
    override fun uncaughtException(t: Thread, e: Throwable) {
        val stackTrace: String = e.stackTraceToString()

        Log.d("TAG", stackTrace)
    }
}

并将其注册到应用程序中:

Thread.setDefaultUncaughtExceptionHandler(ExceptionHandler())

相关问题