0

尝试运行获取我在 Android 模拟器中的当前位置的应用程序时,我得到一个空指针异常。

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

logcat 将我指向提供者的定义,似乎我必须在我的位置管理器实例中使用它来获取当前位置?这是因为我没有任何网络而返回空指针吗?如果是这样,我该如何在 android 模拟器中解决这个问题?我通过 telnet 和 ddms 管理器直接在设备中设置了纬度和经度,但它并没有消除错误。

4

1 回答 1

0

模拟器不支持获取当前位置。因此,在这种情况下,要在模拟器中运行,您需要加载 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

位置.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> 
于 2013-06-30T08:26:09.557 回答