Beginning Android Development with Kotlin, Part Two: Using Android Studio

In this Android Studio tutorial, you’ll learn the fundamental concepts of developing with Android Studio using Kotlin by creating an app to read your fortune. By Kevin D Moore.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Adding Messages

Head to MainActivity.kt and add the following line at the end of onCreate() in MainActivity:

Log.v("FORTUNE APP TAG","onCreateCalled")

Make sure you choose Import for the Log class.

Log.v calls for two parameters: A tag and a message. In this case, you’ve defined the tag as “FORTUNE APP TAG” and the message as “onCreateCalled”.

To try it out, build and run the app so you can see this log message in the Logcat panel.

Logcat log statement

To filter the LogCat contents to show only your message, type onCreateCalled into the search box above the console, like this:

Filtered logcat

When you’re done, remove your search text to see all the log messages again.

Finding Out Why Your App Crashes

Another of Logcat’s useful utilities is the ability to see stacktrace or error messages from app crashes and exceptions. To try it out, add a bug to your perfectly working app to see how that works.

Go to MainActivity.kt and comment out the following line in onCreate():

//fortuneText = findViewById<View>(R.id.fortuneText) as TextView

Build and run the app. Once it launches, click the What’s My Fortune? button on the screen. Oh no! It crashed.

Sad face

How would you go about fixing this if you hadn’t put the bug in on purpose? Logcat to the rescue!

Head back to the Logcat panel — it’ll look something like this:

That sure is a lot of red text, and it’s exactly where to go to sniff around for clues. You’re looking for an exception somewhere. In this case, it’s line 45 in the MainActivity.kt file. LogCat has even helpfully turned that link into a blue hyperlink, and if you click it, it will take you right to the problematic line!

By commenting out fortuneText = findViewById(R.id.fortuneText) as TextView, you created a variable but didn’t assign it a value — hence, the uninitialized property access exception.

Go ahead and uncomment that code then build and run the app. This time there’s no crash!

Logcat is a powerful tool that lets you debug your app’s errors and exceptions.

Where to Go From Here?

You can download the final project using the Download Materials button at the top or bottom of this page.

Practice makes perfect! You’ve learned your way around Android Studio and can now create a project, play around with Gradle, import assets, set up layouts and do some testing.

There’s a lot of cool stuff to learn about Android, and I suggest you start with these next steps:

  • Get familiar with Logcat and its filtering mechanism. Filter by different criteria.
  • If you want to learn more about Android Studio 3, read our What’s New in Android Studio 3 tutorial for more details.
  • This Beginning Android Development tutorial has touched on some of the tools used to build out the layout and UI of your app. To learn more, pour over the official Android documentation for UI, or check out our video course on layouts.
  • Keep coming back to raywenderlich.com. We’ve got some great Android content coming your way over the next days, weeks and months.
  • Talk to other developers. Make use of the forums below and ask questions, share your findings and pick up tips.
Note: Another great next step is to check out our Android Core Concepts tutorials:

That’s all, folks! Give yourself a round of applause and stay tuned for more awesome tutorials from your Android team. :]