getLastKnownLocation()在4. *上运行但在2.3.4上崩溃

时间:2013-09-06 07:17:50

标签: android android-maps android-support-library

我正在使用SupportMapFrgment来确保我的应用程序中Google Maps API的向后兼容性。

似乎在4.1上加载地图就好了,但我在2.3.4上崩溃了。

这是我的代码:

 private String getCurrentLocation() {
        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = service.getBestProvider(criteria, false);
        Location location = service.getLastKnownLocation(provider);



        LatLng userLocation = new LatLng(location.getLatitude(),
                location.getLongitude());

        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses = null;
        String addressText = "";
        try {
            while (addresses == null) {
                addresses = geocoder.getFromLocation(userLocation.latitude,
                        userLocation.longitude, 1);
            }

            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);
                addressText = String.format(
                        "%s, %s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address
                                .getAddressLine(0) : "", address.getLocality(),
                        address.getCountryName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addressText;
    }

这是我检索地图的地方:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

XML布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/location_stuff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/greenish"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/location_header"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/greenish" >

        <RelativeLayout
            android:id="@+id/cancel_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:background="@drawable/done_rounded_btn"
            android:gravity="center" >

            <TextView
                android:id="@+id/retake_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:paddingLeft="7dp"
                android:paddingRight="7dp"
                android:text="@string/cancel"
                android:textAllCaps="true"
                android:textColor="#FFFFFF"
                android:textSize="16sp" />
        </RelativeLayout>

        <TextView
            android:id="@+id/location_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:text="@string/location_map"
            android:textAllCaps="true"
            android:textColor="@color/done_color"
            android:textSize="16sp" />
    </RelativeLayout>

    <fragment
        android:layout_weight="6"
        android:id="@+id/map"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        class="com.google.android.gms.maps.SupportMapFragment"/>


    <RelativeLayout
        android:id="@+id/save_location_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/greenish" >

        <LinearLayout
            android:id="@+id/delete_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:background="@drawable/done_rounded_btn"
            android:orientation="horizontal"
            android:paddingBottom="7dip"
            android:paddingTop="7dip"
            android:weightSum="7" >

            <TextView
                android:id="@+id/delete_txt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:background="@null"
                android:paddingLeft="10dp"
                android:text="@string/save_location"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:contentDescription="@string/hello_world"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:src="@drawable/input_map_white" />
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

和堆栈跟踪:

09-06 10:15:14.366: E/AndroidRuntime(10352): FATAL EXCEPTION: main
09-06 10:15:14.366: E/AndroidRuntime(10352): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.captoom/ro.gebs.captoom.activities.LocationActivity}: java.lang.NullPointerException
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1653)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.os.Looper.loop(Looper.java:130)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.main(ActivityThread.java:3737)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at java.lang.reflect.Method.invokeNative(Native Method)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at java.lang.reflect.Method.invoke(Method.java:507)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at dalvik.system.NativeStart.main(Native Method)
09-06 10:15:14.366: E/AndroidRuntime(10352): Caused by: java.lang.NullPointerException
09-06 10:15:14.366: E/AndroidRuntime(10352):    at ro.gebs.captoom.activities.LocationActivity.getCurrentLocation(LocationActivity.java:131)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at ro.gebs.captoom.activities.LocationActivity.retrieveLocation(LocationActivity.java:98)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at ro.gebs.captoom.activities.LocationActivity.onCreate(LocationActivity.java:80)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617)
09-06 10:15:14.366: E/AndroidRuntime(10352):    ... 11 more
09-06 10:15:14.366: E/AndroidRuntime(10352): [Blue Error Handler] Make Debugging Report file for main
09-06 10:15:14.366: E/AndroidRuntime(10352): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.captoom/ro.gebs.captoom.activities.LocationActivity}: java.lang.NullPointerException
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1653)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.os.Looper.loop(Looper.java:130)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.main(ActivityThread.java:3737)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at java.lang.reflect.Method.invokeNative(Native Method)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at java.lang.reflect.Method.invoke(Method.java:507)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at dalvik.system.NativeStart.main(Native Method)
09-06 10:15:14.366: E/AndroidRuntime(10352): Caused by: java.lang.NullPointerException
09-06 10:15:14.366: E/AndroidRuntime(10352):    at ro.gebs.captoom.activities.LocationActivity.getCurrentLocation(LocationActivity.java:131)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at ro.gebs.captoom.activities.LocationActivity.retrieveLocation(LocationActivity.java:98)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at ro.gebs.captoom.activities.LocationActivity.onCreate(LocationActivity.java:80)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 10:15:14.366: E/AndroidRuntime(10352):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617)
09-06 10:15:14.366: E/AndroidRuntime(10352):    ... 11 more

任何帮助都非常感激。

修改

问题在于设备设置,我只需要启用:

设置 - &gt;应用程序设置 - &gt;发展 - &gt;允许模拟位置

设置 - &gt;位置&amp;安全 - &gt;使用无线网络

感谢您的所有答案。

2 个答案:

答案 0 :(得分:1)

getLastKnownLocation不会造成崩溃。

正如我们从您的stacktrace中看到的那样

09-06 10:15:14.366: E/AndroidRuntime(10352): Caused by: java.lang.NullPointerException
09-06 10:15:14.366: E/AndroidRuntime(10352):    at ro.gebs.captoom.activities.LocationActivity.getCurrentLocation(LocationActivity.java:131)

您正试图在没有任何内容的引用上调用方法或null

如果最后一个位置未知,

getLastKnownLocation可以返回null。你必须处理它。

答案 1 :(得分:1)

getLastKnownLocation方法将返回最后一个缓存位置,因此它将返回null,直到您指定的提供程序至少获得一次更新。您 需要有一个应用程序请求(并接收)至少一次更新 在致电getLastKnownLocation()之前的会话。

所以你需要处理这种情况。

尝试以下,

    location = locationManager.getLastKnownLocation(provider);
    LatLng userLocation = null;
    if (location != null) {

        old_latitude=location.getLatitude();
        old_longitude=location.getLongitude();

        userLocation = new LatLng(old_latitude,old_longitude); 

        this.onLocationChanged(location);
    }