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 (or AVD), choose ToolsAVD Manager. When the AVD Manager appears, click the +Create Virtual Device... button in the lower-left corner 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 Pixel 2, as shown in Figure 1.18. Click Next.

Figure 1.18  Choosing a virtual device

Choosing a virtual device

On the next screen, choose a system image for your emulator. For this emulator, select an x86 Pie emulator and select Next (Figure 1.19). (You may need to follow the steps to download the emulator’s components before you can click Next.)

Figure 1.19  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. For now, name your emulator something that will help you to identify it later and click Finish (Figure 1.20).

Figure 1.20  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 (Ctrl-R). In the Select Deployment Target box that appears, choose the AVD you just configured and click OK. Android Studio will start your virtual device, 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.

If GeoQuiz crashes when launching or when you press a button, useful information will appear in the Logcat tool window. (If Logcat did not open automatically when you ran GeoQuiz, you can open it by clicking the Logcat button at the bottom of the Android Studio window.) Type MainActivity into the search box at the top of the Logcat tool window to filter the log messages. Look for exceptions in the log; they will be an eye-catching red color (Figure 1.21).

Figure 1.21  An example NullPointerException

An example NullPointerException

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 using Logcat in Chapter 3 and about debugging in Chapter 5.)

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 on the emulator. The Back button is shaped like a left-pointing triangle (on older versions of Android, it looks like an arrow that is making a U-turn). Then rerun 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.137.218.215