简体   繁体   中英

Null pointer for defining provider in Android Emulator

I am getting a null pointer exception when trying to run an app that gets my current location in the Android Emulator.

            String provider = LocationManager.NETWORK_PROVIDER;
        Location loc;
        loc = manager.getLastKnownLocation(provider);
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();

The logcat is pointing me to the definition of provider that it seems I must use in my location manager instance to get the current location? Is this returning a null pointer because I don't have any networks? If so, how do I work around this in the android emulator? I set my Latitude and Longitude in the device directly both through telnet and the ddms manager and it does not remove the error.

Emulator not support for the getting the current location. So in that case to run in emulator you need to load the kml.

1. Go to DDMS repository.
2. select current running emulator
3. scroll down load the kml and run
4. run the app on this emulator

location.kml

<?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0">
        <Folder>
            <name>Sisyphus Peak from Wilmot Saddle</name>
            <open>1</open>

                <Placemark>
                    <name>Ashburn</name>
                    <Point>
                        <coordinates>-77.49009453474123,39.01333532337567,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-1</name>
                    <Point>
                        <coordinates>-77.48302697807792,39.00625954769475,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-2</name>
                    <Point>
                        <coordinates>-77.47311743511652,39.00018050891467,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-8</name>
                    <Point>
                        <coordinates>-77.42401333359562,38.97922939627798,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-9</name>
                    <Point>
                        <coordinates>-77.42003559704499,38.97778114326241,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-10</name>
                    <Point>
                        <coordinates>-77.41751898937368,38.97662869617142,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-11</name>
                    <Point>
                        <coordinates>-77.4138528755813,38.97459310765229,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-12</name>
                    <Point>
                        <coordinates>-77.40659262377542,38.97146307963115,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-13</name>
                    <Point>
                        <coordinates>-77.40273606167617,38.96926642020819,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-14</name>
                    <Point>
                        <coordinates>-77.40027538351835,38.9670792991095,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-15</name>
                    <Point>
                        <coordinates>-77.397503898305,38.96335043764458,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-16</name>
                    <Point>
                        <coordinates>-77.39388172459363,38.96070433695704,0</coordinates>
                    </Point>
                </Placemark>
                <Placemark>
                    <name>point-17</name>
                    <Point>
                        <coordinates>-77.3897006097812,38.95805419575319,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-22</name>
                    <Point>
                        <coordinates>-77.36014667981878,38.95215584985607,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-23</name>
                    <Point>
                        <coordinates>-77.35481732900948,38.9512321715715,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-24</name>
                    <Point>
                        <coordinates>-77.34840625130377,38.94965466871911,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-25</name>
                    <Point>
                        <coordinates>-77.34310832451432,38.94852158323671,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-26</name>
                    <Point>
                        <coordinates>-77.3375389604111,38.94651446647728,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-27</name>
                    <Point>
                        <coordinates>-77.33316520209574,38.94474914006099,0</coordinates>
                    </Point>
                </Placemark>

                <Placemark>
                    <name>point-28</name>
                    <Point>
                        <coordinates>-77.3262314555746,38.94404490272601,0</coordinates>
                    </Point>
                </Placemark>
                <Placemark>
                    <name>point-29</name>
                    <Point>
                        <coordinates>-77.31866962122956,38.94550595598364,0</coordinates>
                    </Point>
                </Placemark>

        </Folder>
    </kml> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM