5
Debugging Android Apps

In this chapter, you will find out what to do when apps get buggy. You will learn how to use Logcat, Android Lint, and the debugger that comes with Android Studio.

To practice debugging, the first step is to break something. In MainActivity.kt, comment out the code in onCreate(Bundle?) where you find questionTextView by its ID.

Listing 5.1  Commenting out a crucial line (MainActivity.kt)

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    trueButton = findViewById(R.id.true_button)
    falseButton = findViewById(R.id.false_button)
    nextButton = findViewById(R.id.next_button)
    // questionTextView = findViewById(R.id.question_text_view)
    ...
}

Run GeoQuiz and see what happens. The app will crash and burn almost immediately.

On versions before Android Pie (API 28), an error message appears telling you that the app crashed. On a device running Android Pie, if you watch the screen, you may see the app appear for a brief moment before vanishing without a word. In this case, launch the app again by pressing the GeoQuiz icon on the launcher screen. This time, when the app crashes you will see a message like the one shown in Figure 5.1.

Figure 5.1  GeoQuiz is about to E.X.P.L.O.D.E.

GeoQuiz is about to E.X.P.L.O.D.E.

Of course, you know exactly what is wrong with your app. But if you did not, it might help to look at your app from a new perspective.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.145.114