从Big Nerd Ranch学习Java编程,我试图利用Google Map API开发一个简单的应用程序,但我遇到了一个错误,说"MapsActivity不是抽象的,不会覆盖ConnectionCallbacks中的抽象方法onConnectionSuspended(int("。抽象类似乎不是可行的方法,因为组件无法实例化。
package com.example.test.empower;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderClient;
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 android.Manifest;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
protected GoogleApiClient mGoogleApiClient;
TextView textView_location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
textView_location = (TextView) findViewById(R.id.textView_location);
// Font Awesome for Location Icon
Typeface font = Typeface.createFromAsset(getAssets(), "fa_regular_400.ttf" );
TextView fa_map_marker = (TextView)findViewById(R.id.map_marker);
fa_map_marker.setTypeface(font);
// 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);
textView_location.setText("First");
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
}
public void onConnected(Bundle bundle)
{
// Create array of permission
String mPerms[] = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
// Checks for permission
if(checkSelfPermission(mPerms[0]) == PackageManager.PERMISSION_DENIED) {
requestPermissions(mPerms, 1);
}
else {
// Enable Location
mMap.setMyLocationEnabled(true);
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if(mLastLocation != null){
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
// Add a marker in Sydney and move the camera
LatLng myLocation = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(myLocation).title("You're here."));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
mMap.animateCamera( CameraUpdateFactory.zoomTo( 12.0f ) );
textView_location.setText(String.valueOf(latitude));
}
}
}
}
这是activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="500dp"
class="com.google.android.gms.maps.SupportMapFragment"/>
<Button
android:id="@+id/round_alert_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="170dp"
android:background="@drawable/round_alert_button"
android:drawableStart="@drawable/handshake_100x66"
android:paddingLeft="2dp"
android:paddingTop="12dp" />
<TextView
android:id="@+id/textView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="240dp"
android:text="Get Assistance"
android:textColor="#000000"
android:fontFamily="Proxima Nova"
android:textStyle="bold"
android:textSize="16dp"/>
<com.example.denise.empower.FontAwesome
android:id="@+id/map_marker"
android:text="@string/fa_map_marker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="265dp"
android:layout_marginRight="35dp"
android:textSize="18sp"
android:textColor="#000000"
/>
<TextView
android:id="@+id/textView_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="265dp"
android:text="Singapore"
android:textColor="#878787"
android:fontFamily="Proxima Nova"
android:textStyle=""
android:textSize="12dp"/>
<TextView
android:id="@+id/textView_description"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="290dp"
android:text="Do not subject yourself to a silent victim, reach out to our nearest law-enforcer."
android:textColor="#878787"
android:fontFamily="Proxima Nova"
android:textStyle=""
android:textSize="12dp"
android:textAlignment="center"/>
</FrameLayout>
GoogleMapsApiClient.ConnectionCallbacks
是一个具有两个抽象方法的interface
:onConnected()
和onConnectionSuspended()
。因为您正在实现interface
,所以您必须同时实现这两个方法,或者声明您的类abstract
,以指示子类将执行该实现。
正如您所提到的,您正在进行实际的实现,因此抽象并不是一种可行的方法。在你班的某个地方,你必须重写onConnectionSuspended()
。如果您不打算使用该回调,只需将方法的主体留空,但声明必须包含在类中。