Mobile Programming Lecture 4 Debugging
Lecture 2 Review How do you make the android:inputtype attribute of an EditText both textcapwords and textmultiline? Why should you use a @string resource for TextViews instead of hardcoding the string? If you use the same android:onclick value for multiple views, how do you determine which one was clicked?
Agenda Debugging using Toast... LogCat Debug Perspective Importing existing projects into Android Studio Lab debugging assignment
Debugging using Toast... A Toast is an easy way to debug your app... sometimes
Try not to debug using Toast! It's slower especially if you're using multiple Toasts It doesn't persist after the Toast is gone, you may not have seen all of the debug information, then you'll have to run it again Sometimes the code for a Toast will be correct, but the Toast just won't show! depends on the state of the application
Debugging - LogCat Log.i("HelloWorldActivity", "This line has been executed"); This is the message Tag
Debugging - LogCat Log.i("HelloWorldActivity", "This line has been executed"); This is the message
Debugging - LogCat Log.i("HelloWorldActivity", "This line has been executed"); Log.i("HelloWorldActivity", "Value of x = " + x);
Debugging - LogCat Log.i("HelloWorldActivity", "This line has been executed"); Log.i("HelloWorldActivity", "Value of x = " + x); This is the message. Prints the value of x to the Log
Debugging - LogCat A good convention is to declare a TAG constant in your class
Debugging - LogCat private static final String TAG = "HelloWorldActivity"; Log.i(TAG, "This line has been executed"); Log.i(TAG, "Value of x = " + x);
Debugging - LogCat Open the LogCat view if it's not already open in Android Studio Open the Active Tool Window and ensure Android Monitor is selected Select the logcat tab In the device dropdown ensure the device you wish to view log output for is selected You can then select the debuggable app whose output should be displayed You can choose to display output only for the selected app, remove filters or modify the current filter To enter a custom filter, select Enter Filter Configuration and populate the appropriate fields, e.g. Filter Name: HelloWorldActivity Log Tag: HelloWorldActivity Click OK Your debug messages should now show up
Debugging - LogCat You can view LogCat information up until you close Android Studio (or probably until you run out of memory dedicated to LogCat)
Debugging - Using Breakpoints Another way to debug is by using breakpoints, which you may already be familiar with from an IDE other than Android Studio Add breakpoints to lines in your code where you want to pause your program To start debugging, you can do one of the following Press Shift + F9 Select Run > Debug <module> If your application gets to your breakpoint, it will pause there
Debugging - Debug Tool Window If the Debug Tool Window isn t displayed automatically you can display it by pressing Alt + 5 If you have never launched your app for debugging, you may not be able to access the Debug Tool Window The Frames section of the Debug Tool Window represents the call stack It allows you to select various method calls up to and including the method containing your breakpoint The debugger will automatically attempt to decompile calls to system methods (i.e. for classes not owned by your project e.g. Activity method calls) The Variables section allows you to inspect and modify the values of different variables accessible within the current Frame Buttons in the Debug Tool Window allow you to continue or step through the rest of the code Resume, Step Into, Step Over, etc
Exporting projects for submission To export your Android Studio projects for submission: Simply compress your project folder (tar or zip, tar is the preferred submission format) If you do not know how to do this a quick Google search should provide instructions and/or tools for your specific OS
Importing projects into Android Studio Most code examples that I post will be.zip (or.gz) archive files To import an existing project into Android Studio Download and extract the archive file Navigate to File > New > Import Project... Browse to the extracted directory and select it Click OK Or. You can attempt to open the project directly File > Open Browse to the extracted directory and select it Click OK I may include a README file in the root directory with instructions that you need to follow
References The Busy Coder's Guide to Android Development - Mark Murphy Android Developers The Mobile Lab at Florida State University