Using LogCat to understand crashes

Discussion in 'Tutorials' started by Vas, Sep 6, 2013.

  1. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    This tutorial is long overdue.
    LogCat is absolutely essential in application development. Now that a lot of you started adding custom code, you will undoubtedly come across application crashes. Usually, the phone will not give you any specifics other than "Your app has crashed". You will be left wondering why with no other information. However, additional information does exist, and you will find it in LogCat. LogCat is a tab in Eclipse that tells you what's going on with your phone. Every process and every app is logged and you can find that log in LogCat. This is where you will find additional information about your crash.

    I have prepared an example from a recent tutorial that will show you how to use LogCat.
    First you need to locate LogCat in Eclipse.

    logcat1.png

    If you don't see it there, go to Window>Show View>Other>LogCat.

    I have already got my application to crash, so the error is already logged in LogCat. Expand the window to make it easier to see. You will find many lines of different colours, with different messages. What you are looking for are lines that are marked with your application's package name. If it is a crash, you will see it in red. You can either sift through the messages manually, or create a filter (if LogCat has not done it for you already). It's simple, just explore the controls that LogCat offers. They are all labelled, so you can't get lost.

    logcat2.png

    Now that I see the red messages (it's called a stack trace) that are tagged with my app's package name, I can review the details of the crash. The second red line is the most informative one (underneath FATAL EXCEPTION: main). It will contain the entire error message that will tell you what the issue is most of the time. In my case it says (note, you can copy/paste these lines directly with ctrl+c/ctrl+v):

    09-06 00:02:25.092: E/AndroidRuntime(23830): java.lang.RuntimeException: Unable to start activity ComponentInfo{Test.cardslwp/Test.cardslwp.Splash}: java.lang.RuntimeException: native typeface cannot be made

    You still need to understand what this error means, but it's a lot better than "Your application has crashed". You can use the forums, or Google, or what have you to start looking for a solution, because now you have a lead. If you analyze the entire stack trace, you will find a line that tells you exactly where the error came from:

    09-06 14:32:10.121: E/AndroidRuntime(3146): at Test.cardslwp.Splash.onCreate(Splash.java:28)

    This line tells you the package, class, method and the line number of the problematic code (the last number in brackets). Now you can go back and see what line of code is causing the problem. In our case it's this line:

    Code (java):
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/SHOWG.TTF");

    The error says that native typeface cannot be made, so that means there is some issue with the font. That's right, because I intentionally deleted the font from my assets folder. The typeface cannot be created because there is no font asset to create from, hence the application crashes.

    Now that we figured out the problem, we can fix it. Either include a font into the assets folder, or just comment out the line and don't use a custom font. If you go with the latter, make sure you comment out all the other lines that use this variable (font).

    That's it for this example, but this above procedure of using LogCat is essential to dealing with crashes and errors. From now on, if you have a crash, please post your stack trace instead of posting "My app crashed" :)

    Stickied.
    NewKid and fatos like this.
  2. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    I made one live wallpaper to crash to see how LogCat works but the error is not logged whith LogCat,.only Problems and Error log are showing my errors and not LogCat
  3. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    Was LogCat completely blank, or did it have other logs of other processes? If it's blank, then LogCat doesn't recognize that your device is connected. In this case, you should restart Eclipse.
  4. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Yes it was blank,I already restart Eclipse but again LogCat is blank....Do I have a device/emulator in order LogCat to work

    UPDATE: Now is working :)
  5. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100
    I have a pretty slow computer so my virtual device is prominent on lagging, I have an android device but the usb is broken so I have no way of plugging it into my computer and testing apps with it through eclipse. I use Wifi Transfer to transfer my applications to my device to test them and that is how I've been doing it ever since I started developing live wallpapers. I get multiple crash reports on my Google Developer Console for many of my applications and I would like to use the crash reports to fix my live wallpapers, is there anyway I can use the information given to me on my Google Developer Console to fix my applications through eclipse with logcat? If there isn't please refer me to an alternative method as I would like to continue in growth of my users. Thank you for your time.
  6. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    I think there may be a logcat app on the market.
  7. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100
    :eek:
  8. Maximus Seasoned Vet

    Member Since:
    Jan 16, 2012
    Message Count:
    122
    Likes Received:
    42
    Trophy Points:
    100

    Upgrade, my man! Canadian contracts can now only be a maximum of 2 years! It speeds up the testing process SO much. You don't have to export+sign the application, move onto your phone, and then find it on your phone via a file manager and install. You just have the project open in Eclipse and click "Run as Android Application" and it installs it on your phone immediately and you can see if it works/doesn't work or w/e. Plus LogCat reads it.

    LogCat is best cat.
    Joshua likes this.
  9. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100

    Wow, that'd be awesome, cause I actually have 10 wallpapers waiting to be coded and tested right now >,< just because of that main reason. Luckily I am going to be upgrading my pc within a week or so, and I got a working android phone via usb sooo I'm good to go. I just don't know quite know how to use the LogCat code to fix the errors >,< Is there anyway I could learn how to interpret it?
  10. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    @Joshua Google the error messages that logcat gives you.
  11. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100

    :eek: that simple huh
  12. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100
    No active compatible AVD's or devices found. Relaunch this configuration after connecting a device or starting an AVD....
    I did this, still could not get the application to load onto my device.
  13. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
  14. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100
  15. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    I don't know. Make sure you have USB debugging on. Logcat won't make your app load. It will only show what goes on in your phone when it's plugged into the computer.
  16. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100
    Okay


    Sent from my iPhone using Tapatalk - now Free
  17. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Vas can you make a tutorial, I dont know what but just do something...we are waiting for LWC 3.0 but until then teach us something, maybe a new feature of LWC 3.0
  18. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Vas can you make a tutorial, I dont know what but just do something...we are waiting for LWC 3.0 but until then teach us something, maybe a new feature of LWC 3.0
  19. Joshua Seasoned Vet

    Member Since:
    Jul 12, 2013
    Message Count:
    116
    Likes Received:
    16
    Trophy Points:
    100
    Hey guys, I keep on getting this one specific crash on my live wallpapers, even when I do not put them through eclipse. I am not a coder, and yes I am currently studying java online but it will take me some time to actually put my knowledge into actual practicality.
    For my Live Wallpaper Creator 2.6 I'm using, Java JDK 1.6.0_43 and the Android SDK of course.
    But these lines of code keep appearing,

    Code (text):
    java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412cc2e0
    at android.graphics.Canvas.throwIfRecycled(Canvas.java:1026)
    at android.graphics.Canvas.drawBitmap(Canvas.java:1065)
    at com.sbg.lwc.SBLiveWallpaper$LwpEngine.draw(SBLiveWallpaper.java:879)
    at com.sbg.lwc.SBLiveWallpaper$LwpEngine.drawFrame(SBLiveWallpaper.java:726)
    at com.sbg.lwc.SBLiveWallpaper$LwpEngine.onVisibilityChanged(SBLiveWallpaper.java:630)
    at android.service.wallpaper.WallpaperService$Engine.updateSurface(WallpaperService.java:722)
    at android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:772)
    at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1015)
    at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4794)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)
    It'd be great to get some help, I hope to represent the Live Wallpaper Community a little better than this.
  20. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    I'll have to think of what else I could show you.

Share This Page