获取最后一个位置不会返回位置

时间:2016-05-25 20:20:10

标签: java android location

我无法返回我的上一个已知位置并调用addMarker。它说我的错误堆栈中没有请求适当的API。这是一个很大的计划,所以我只是把重要的部分放在这里:

public LatLng getLastKnownLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
    Location lastLocation = FusedLocationApi.getLastLocation(client);
    return new LatLng(lastLocation.getLatitude(),lastLocation.getLongitude());

}

来自:

mMap.addMarker(newMarkerOptions().position(getLastKnownLocation()).title(memoryString));

我的错误:

  

23126-23126 / com.example.kevin.memories E / AndroidRuntime:FATAL EXCEPTION:main                                                            处理:com.example.kevin.memories,PID:23126                                                            java.lang.NullPointerException:未请求适当的Api。                                                                在com.google.android.gms.common.internal.zzaa.zzb(未知来源)                                                                在com.google.android.gms.internal.zzof.zza(未知来源)                                                                在com.google.android.gms.location.LocationServices.zzi(未知来源)                                                                在com.google.android.gms.location.internal.zzd.getLastLocation(未知来源)                                                                在com.example.kevin.memories.MapsActivity.getLastKnownLocation(MapsActivity.java:134)                                                                在com.example.kevin.memories.MapsActivity.onMapReady(MapsActivity.java:107)                                                                在com.google.android.gms.maps.SupportMapFragment $ zza $ 1.zza(未知来源)                                                                在com.google.android.gms.maps.internal.zzo $ zza.onTransact(未知来源)                                                                在android.os.Binder.transact(Binder.java:380)                                                                在wv.a(:com.google.android.gms.DynamiteModulesB:82)                                                                at maps.ag.t $ 5.run(未知来源)                                                                在android.os.Handler.handleCallback(Handler.java:739)                                                                在android.os.Handler.dispatchMessage(Handler.java:95)                                                                在android.os.Looper.loop(Looper.java:145)                                                                在android.app.ActivityThread.main(ActivityThread.java:5835)                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                在java.lang.reflect.Method.invoke(Method.java:372)                                                                在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)   23126-23745 / com.example.kevin.memories E / DynamiteModule:无法加载模块描述符类:未找到类“com.google.android.gms.dynamite.descriptors.com.google.android.gms.googlecertificates。 ModuleDescriptor“on path:DexPathList [[zip file”/data/app/com.example.kevin.memories-1/base.apk"],nativeLibraryDirectories=[/vendor/lib,/ system / lib]]

我错过了什么吗?

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    Toolbar toolbar = (Toolbar) findViewById(R.id.login_toolbar);
    toolbar.setTitle("Login");

    if (client == null) {
        GoogleApiClient client = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        loginButtons();
    }
    loadMap();
}

1 个答案:

答案 0 :(得分:0)

我收到相同的日志错误here在我的情况下,GoogleApiClient没有返回null。获取非空的最后位置我没有问题。我用吐司验证了这一点。我唯一的问题是永远不会调用标记显示代码。如果您的客户端仍然返回null,请查看我的实现。 这是为了获取最后一个位置。日志错误本身的解决方案是downgrading 9.0.83 Google Play Services