200 likes | 344 Views
Mobile Programming Lecture 3. Debugging. Lecture 2 Review. What widget would you use to allow the user to enter a yes/no value a range of values from 1 to 100 What's the benefit of a Relative Layout over LinearLayout? How many ways can you set an event listener?. Lecture 2 Review.
E N D
Mobile Programming Lecture 3 Debugging
Lecture 2 Review • What widget would you use to allow the user to enter • a yes/no value • a range of values from 1 to 100 • What's the benefit of a RelativeLayout over LinearLayout? • How many ways can you set an event listener?
Lecture 2 Review • How do you make the android:inputType attribute of an EditText both textCapCharacters and 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 Eclipse • 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 • LogCat shows the stack traces, diagnostic information from the operating system. • You can use LogCat in Eclipse easily: • Log.i("HelloWorldActivity", "This line has been executed"); This is the message Tag 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 Eclipse • Window > Show View > LogCat • Under Saved filters, click the + button to add a new filter • Enter the following (modify to match your app) • Filter Name: HelloWorldActivity • by Log Tag: HelloWorldActivity • Click OK • Your debug messages should now show up • If they're not showing up, double check your filter (or advance a few slides in this presentation)
Debugging - LogCat You can view LogCat information up until you close Eclipse (or probably until you run out of memory dedicated to LogCat)
Debugging - Debug Perspective Another way to debug is by using breakpoints, which you may already be familiar with from an IDE other than Eclipse • 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 F11 • Run > Debug If your application gets to your breakpoint, it will pause there
Debugging - Debug Perspective • If it asks whetheryou want to switch to Debug Perspective, say yes • In the Expressions View of the Debug Perspective, you can add variables to see what their values are at the breakpoint • e.g., if you have int x somewhere in your code, try adding x to • You may have to open the view first • Window > Show View > Expressions • Buttons in the Debug View allow you to continue or step through the rest of the code • Resume, Step Into, Step Over, etc
Exporting projects from Eclipse To export a project from Eclipse • Right click your project > Export > General > Archive File • Next • Select your project if necessary • Options > Save in tar format preferably (zip will work as well) • Browse ... • Finish Submit your homework assignments and projects in these formats
Importing projects into Eclipse Most code examples that I post will be .tar (or .zip) archive files To import an existing project into Eclipse • Download the archive file • Navigate to File > Import ... > General > Existing Project into workspace • Choose "Select archive file" • Browse for the downloaded file and select it • Finish 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