benwad
benwad

Reputation: 6594

Logging and crash stack traces not showing in Android Studio

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:

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:

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?

Upvotes: 8

Views: 15573

Answers (4)

melikaafrakhteh
melikaafrakhteh

Reputation: 367

You should define a class that implement UncaughtExceptionHandler and use stackTraceToString in Kotlin:

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

and register it in your application:

Thread.setDefaultUncaughtExceptionHandler(ExceptionHandler())

Upvotes: 0

Abdul Samad
Abdul Samad

Reputation: 524

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.

Why does android logcat not show the stack trace for a runtime exception?

Upvotes: 2

user4115463
user4115463

Reputation:

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

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 .

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

Upvotes: 13

QArea
QArea

Reputation: 4981

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.

Upvotes: 4

Related Questions