我正在尝试在Android应用中使用位置服务,但是当我创建GoogleApiClient
实例时(按照Google here的建议),应用程序崩溃时出现此错误:
--------- beginning of crash
11-01 16:11:44.805 10225-10225/com.company.appname E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.appname, PID: 10225
java.lang.AbstractMethodError: abstract method "com.google.android.gms.common.api.Api$zze com.google.android.gms.common.api.Api$zza.zza(android.content.Context, android.os.Looper, com.google.android.gms.common.internal.zzq, java.lang.Object, com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks, com.google.android.gms.common.api.GoogleApiClient$OnConnectionFailedListener)"
at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)
at com.company.appname.MapsActivity.initLocationStuff(MapsActivity.java:50)
at com.company.appname.MapsActivity.onCreate(MapsActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6757)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
这是我的班级:
package com.company.appname;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
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;
import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.maps.android.heatmaps.HeatmapTileProvider;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
private HeatmapTileProvider heatmapProvider;
private TileOverlay heatmapOverlay;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
initLocationStuff();
}
protected void initLocationStuff() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle connectionHint) {
Log.d("DEV", "connected");
}
public void onConnectionFailed(ConnectionResult result) {
Log.d("DEV", "Google api connection failed " + result.toString());
}
public void onConnectionSuspended(int i) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
final MapsActivity self = this;
mMap = googleMap;
new EnvelopeGetter("https://192.168.1.177:8080/api/upvotes", new EnvelopeGetter.CompletionHandler() {
public void onSuccess(JSONObject response) {
try {
ArrayList<Upvote> upvotes = Upvote.fromJSONArray(response.getJSONArray("upvotes"));
ArrayList<LatLng> coordinates = new ArrayList<>();
for(Upvote upvote : upvotes) {
coordinates.add(upvote.coordinate);
}
self.addHeatMap(coordinates);
Log.d("DEV", upvotes.toString());
} catch(Exception e) {
e.printStackTrace();
}
}
public void onFail(String errorMessage) {
Log.d("DEV", errorMessage);
}
});
}
private void addHeatMap(ArrayList<LatLng> points) {
Log.d("DEV", "ADDING HEAT MAP " + points.size());
mMap.moveCamera(CameraUpdateFactory.newLatLng(points.get(0)));
heatmapProvider = new HeatmapTileProvider.Builder().data(points).build();
heatmapOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(heatmapProvider));
}
}
这是我的build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.company.appname"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.google.maps.android:android-maps-utils:0.5+'
compile 'com.google.android.gms:play-services-location:9.0.1'
compile 'com.github.kevinsawicki:http-request:6.0'
testCompile 'junit:junit:4.12'
}
如果我注释掉initLocationStuff
方法的内容,则该应用不会崩溃。
答案 0 :(得分:2)
这可能是由于播放服务API更新不匹配引起的。