8

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?

benwad
  • 6,414
  • 10
  • 59
  • 93
  • 4
    I often find that the package-based filtering prevents log output from showing in the logcat, for some reason. For diagnosing your ANR, I recommend: removing all filtering in the logcat, and switching the log level to Warning (not Error). Then scroll through the output to see if any details on the ANR show up. – stkent Jan 08 '15 at 14:01
  • I'm not sure, but I had similar issues. Just click Run -> Resume.. – Martin Pfeffer Jan 08 '15 at 14:29
  • I have a similar problem with AS 1.0 when I filter by my app packagename I see nothing in Logcat. Removing the filter shows everything from the the device. – Mike Fosker Jan 09 '15 at 14:29
  • Thanks guys, yeah string filtering seems to be working. I'm still not going directly into a stack trace when I crash but at least I can see the trace in Logcat so that's something! Probably best to leave this question open unless someone figures out what's going on. – benwad Jan 12 '15 at 17:54

4 Answers4

13

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.

4

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.

QArea
  • 4,955
  • 1
  • 12
  • 22
  • Thanks for pointing out the filter, it appears my filter was accidentally set to Firebase so almost no log appear on LogCat – Neon Warge Aug 24 '17 at 08:11
  • @NeonWarge You saved my day!! Why the hell is the filter set to firebase anyhow? – Ishaan Nov 16 '17 at 18:28
2

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?

Community
  • 1
  • 1
Abdul Samad
  • 526
  • 5
  • 13
0

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())
melikaafrakhteh
  • 305
  • 1
  • 2
  • 8