Running on the Emulator

To run an Android application, you need a device – either a hardware device or a virtual device. Virtual devices are powered by the Android emulator, which ships with the developer tools.

To create an Android virtual device (AVD), choose ToolsAndroidAVD Manager. When the AVD Manager appears, click the Create Virtual Device... button on the lefthand side of the window.

In the dialog that appears, you are offered many options for configuring a virtual device. For your first AVD, choose to emulate a Nexus 5, as shown in Figure 1.15. Click Next.

Figure 1.15  Choosing a virtual device

Choosing a virtual device

On the next screen, choose a system image that your emulator is based on. For this emulator, select an x86 Lollipop emulator and select Next (Figure 1.16).

Figure 1.16  Choosing a system image

Choosing a system image

Finally, you can review and tweak properties of the emulator. You can also edit the properties of an existing emulator later on. For now, name your emulator something that will help you to identify it later on and click Finish (Figure 1.17).

Figure 1.17  Updating emulator properties

Updating emulator properties

Once you have an AVD, you can run GeoQuiz on it. From the Android Studio toolbar, click the Run button (it looks like a green “play” symbol) or press Control+R. Android Studio will find the virtual device you created, start it, install the application package on it, and run the app.

Starting up the emulator can take a while, but eventually your GeoQuiz app will launch on the AVD that you created. Press buttons and admire your toasts. (Note that if the app launches and you are not around, you may have to unlock the AVD when you come back. The AVD works like a real device, and it will lock itself after a time.)

If GeoQuiz crashes when launching or when you press a button, useful information will appear in the LogCat view in the Android DDMS tool window. (If LogCat did not open automatically when you ran GeoQuiz, you can open it by clicking the Android button at the bottom of the Android Studio window.) Look for exceptions in the log; they will be an eye-catching red color, as shown in Figure 1.18.

Figure 1.18  An example NullPointerException at line 21

An example NullPointerException at line 21

Compare your code with the code in the book to try to find the cause of the problem. Then try running again. (You will learn more about LogCat and debugging in the next two chapters.)

Keep the emulator running; you do not want to wait for it to launch on every run. You can stop the app by pressing the Back button (the arrow that is making a U-turn). Then re-run the app from Android Studio to test changes.

The emulator is useful, but testing on a real device gives more accurate results. In Chapter 2, you will run GeoQuiz on a hardware device. You will also give GeoQuiz more geography questions with which to test the user.

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

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