Coding Your Application Coding Your Application

147

Chapter 5: Coding Your Application

Figure 5-17: Setting the application up as debuggable. Application tab Choose True. To start the debugger, choose Run➪Debug or press F11. This tells the ADT and Eclipse to install the application onto the emulator or device and then attach the debugger to it. If your emulator is not already brought to the foreground, do that now. The application installs, and now you see a screen that looks like what is shown in Figure 5-18. This screen informs you that the ADT and the emulator are trying to make a connection behind the scenes. Figure 5-18: The emulator waiting for the debugger to attach. 148 Part II: Building and Publishing Your First Android Application The emulator might sit for a moment while the debugger attaches. After the debugger attaches, it runs your application code and stops when it finds its first breakpoint. Upon doing so, you are presented with a dialog box asking whether it’s okay to open the Debug perspective, as shown in Figure 5-19. Click Yes. Figure 5-19: Enabling the Debug perspective. You should now be at a breakpoint, as shown in Figure 5-20. You can hover over variables and see their values. Figure 5-20: The Debug perspective explained. Resume Disconnect Step execution navigation Hovering over a variable while debugging Code execution stopped at your breakpoint 149

Chapter 5: Coding Your Application

Hover your cursor over the mAudioManager variable. You should see that it is currently null because you had commented out the code, as shown in Figure 5-20. You can also step through the execution of the code by operating the debug navigation, as shown in Figure 5-20. If you click the Continue button or press F8 three times, you can see the Debug perspective change and eventually say source not found. Open the emulator, and you can see that your application has crashed, as shown in Figure 5-21. In the Android Market, users have come to know of this screen as the Force Close, or FC, screen. A force close happens when a run-time exception occurs that is not handled inside your code. Figure 5-21: A Force Close dialog box presented due to a run-time exception. To disconnect the debugger, click the Disconnect button, as shown in Figure 5-20. Return to the Java perspective, and uncomment line ➝ 3 from Listing 5-7 in the MainActivity.java file to ensure that the application builds successfully. Checking logic errors Computers do exactly what you tell them to do, and this little smartphone isn’t smart enough to understand what’s right or wrong when it comes to literal logic. An example of an error in literal logic is demonstrated in Listing 5-8. 150 Part II: Building and Publishing Your First Android Application Listing 5-8: Code That Doesn’t Check the Phone for Silent Mode Toggles the UI images from silent to normal and vice versa. private void toggleUi { ImageView imageView = ImageView findViewByIdR.id.phone_icon; Drawable newPhoneImage; if mPhoneIsSilent { ➝ 11 newPhoneImage = getResources.getDrawableR.drawable.phone_silent; } else { newPhoneImage = getResources.getDrawableR.drawable.phone_on; } imageView.setImageDrawablenewPhoneImage; } Override protected void onResume { super.onResume; checkIfPhoneIsSilent; ➝ 26 toggleUi; }; ➝ 11 This line checks to see whether the phone is currently in the silent mode. ➝ 26 For the toggleUi method to properly display the correct user interface to the user, the application has to know what state the ringer is currently in. On this line, I accidentally commented out the checkIfPhoneIsSilent method, which updates the class-level mPhoneIsSilentVariable. Because this occurs in the onResume method, the user could leave the app, change the ringer state through the settings of the phone, and then return to the app, and the app would be in an incorrect state simply because of a logic error Using a debugger, you could attach a breakpoint on the first line of the toggleUi method to inspect the various variables that help make the logic calls. At that time, you would notice that mPhoneIsSilent is not being set. 151

Chapter 5: Coding Your Application