gpt4 book ai didi

java - 如果不创建抽象类,则无法实现 Google Map API

转载 作者:行者123 更新时间:2023-12-02 02:01:51 26 4
gpt4 key购买 nike

从 Big Nerd Ranch 学习 Java 编程,我正在尝试利用 Google Map API 开发一个简单的应用程序,但遇到错误,提示“MapsActivity 不是抽象的,并且不会覆盖 ConnectionCallbacks 中的抽象方法 onConnectionSuspished(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.ConnectionCallbacksinterface有两个抽象方法:onConnected()onConnectionSuspended()。由于您正在实现接口(interface),因此您必须实现这两个方法,或者声明您的类abstract以指示子类将执行该实现。

正如您所提到的,您正在执行实际的实现,因此抽象不是可行的方法。在类中的某个位置,您必须重写 onConnectionSuspished()。如果您不打算使用该回调,只需将方法主体留空即可,但声明必须包含在您的类中。

关于java - 如果不创建抽象类,则无法实现 Google Map API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51461821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com