不断收到 FATAL ECEPTION:GoogleApiClient 尚未连接
Posted
技术标签:
【中文标题】不断收到 FATAL ECEPTION:GoogleApiClient 尚未连接【英文标题】:Keep getting FATAL ECEPTION: GoogleApiClient is not connected yet 【发布时间】:2015-12-10 09:58:32 【问题描述】:我正在处理的应用程序总是给我这个错误
致命异常:主进程:com.map.locationservice,PID:12739 java.lang.IllegalStateException:GoogleApiClient 尚未连接。
下面是我的代码和 androidManifest.xml
package com.map.locationservice;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener, LocationListener
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LocationRequest mLocationRequest;
boolean mRequestingLocationUpdates = true;
double Latitude = 0;
double Longitude = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(2 * 1000); // 2 second, in milliseconds
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
protected void onStart()
mGoogleApiClient.connect();
super.onStart();
@Override
protected void onStop()
mGoogleApiClient.disconnect();
super.onStop();
@Override
protected void onPause()
super.onPause();
stopLocationUpdates();
@Override
protected void onResume()
super.onResume();
//Connect GoogleApiClient
mGoogleApiClient.connect();
protected void createLocationRequest()
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
public void onMapReady(GoogleMap google)
startLocationUpdates();
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(Latitude, Longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
@Override
public void onConnected(Bundle bundle)
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mRequestingLocationUpdates)
startLocationUpdates();
if (mLastLocation != null)
Latitude = mLastLocation.getLatitude();
Longitude = mLastLocation.getLongitude();
@Override
public void onConnectionSuspended(int bundle)
mGoogleApiClient.connect();
@Override
public void onLocationChanged(Location location)
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
mGoogleApiClient.connect();
protected void startLocationUpdates()
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
protected void stopLocationUpdates()
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.map.locationservice">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
知道为什么我总是遇到同样的错误吗?
【问题讨论】:
【参考方案1】:访问本教程并仔细按照步骤操作。它对我有用。 Create Google Map Application
在此之前,您是否在谷歌开发者控制台中创建了您的项目,并在您的包中添加了您的 SHA1 密钥并检索了您的 API 密钥..?
【讨论】:
是的,我确实有 API 密钥,在调用 startLocationUpdates() 之后发生了 FATAL EXCEPTION 离开您当前的项目,尝试创建一个新项目。如果您使用的是android studio,请按照我给您的视频链接,或者按照这个有完整解释的教程:tutorialspoint.com/android/android_google_maps.htm。如果您使用的是 eclipse,请遵循本教程:examples.javacodegeeks.com/android/core/google-maps/…以上是关于不断收到 FATAL ECEPTION:GoogleApiClient 尚未连接的主要内容,如果未能解决你的问题,请参考以下文章
kettle 7.0 部署报错 A JAVA Eception has occurred
在Oracle中,我一直收到PLSQL的Fatal ErrorReset连接。
Arval SQLException: FATAL: 抱歉,postgres 中已经有太多客户端
FlutterFcmService("number"): Fatal: 找不到回调