2017-01-22 8 views
1

Я пытаюсь сделать отображение карты Google рядом с местами, используя модификацию после this tutorial.Анонимный класс не является абстрактным и не переопределяет (Android + Retrofit)

Я получаю эту ошибку:

Error:(158, 46) error: is not abstract and does not override abstract method onFailure(Call,Throwable) in Callback

Error:(159, 13) error: method does not override or implement a method from a supertype

Я пытался реализовать методы, используя Alt + Enter, но он получает отличается от руководства, и это портит код.

Вот мое главное (это то же самое из учебника)

MapsActivity.java

package example.googlemapsapp; 

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Build; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 


import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GoogleApiAvailability; 
import com.google.android.gms.common.api.GoogleApiClient; 
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.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.util.HashMap; 

import example.googlemapsapp.POJO.Example; 
import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 
import retrofit2.Retrofit; 
import retrofit2.converter.gson.GsonConverterFactory; 


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

    private GoogleMap mMap; 
    double latitude; 
    double longitude; 
    private int PROXIMITY_RADIUS = 10000; 
    GoogleApiClient mGoogleApiClient; 
    Location mLastLocation; 
    Marker mCurrLocationMarker; 
    LocationRequest mLocationRequest; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 

     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      checkLocationPermission(); 
     } 

     //show error dialog if Google Play Services not available 
     if (!isGooglePlayServicesAvailable()) { 
      Log.d("onCreate", "Google Play Services not available. Ending Test case."); 
      finish(); 
     } 
     else { 
      Log.d("onCreate", "Google Play Services available. Continuing."); 
     } 

     // 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); 
    } 

    private boolean isGooglePlayServicesAvailable() { 
     GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); 
     int result = googleAPI.isGooglePlayServicesAvailable(this); 
     if(result != ConnectionResult.SUCCESS) { 
      if(googleAPI.isUserResolvableError(result)) { 
       googleAPI.getErrorDialog(this, result, 
         0).show(); 
      } 
      return false; 
     } 
     return true; 
    } 

    /** 
    * 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 googleMap) { 
     mMap = googleMap; 
     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 

     //Initialize Google Play Services 
     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { 
       buildGoogleApiClient(); 
       mMap.setMyLocationEnabled(true); 
      } 
     } else { 
      buildGoogleApiClient(); 
      mMap.setMyLocationEnabled(true); 
     } 

     Button btnRestaurant = (Button) findViewById(R.id.btnRestaurant); 
     btnRestaurant.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       build_retrofit_and_get_response("restaurant"); 
      } 
     }); 

     Button btnHospital = (Button) findViewById(R.id.btnHospital); 
     btnHospital.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       build_retrofit_and_get_response("hospital"); 
      } 
     }); 

     Button btnSchool = (Button) findViewById(R.id.btnSchool); 
     btnSchool.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       build_retrofit_and_get_response("school"); 
      } 
     }); 
    } 

    private void build_retrofit_and_get_response(String type) { 

     String url = "https://maps.googleapis.com/maps/"; 

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(url) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     RetrofitMaps service = retrofit.create(RetrofitMaps.class); 

     Call<Example> call = service.getNearbyPlaces(type, latitude + "," + longitude, PROXIMITY_RADIUS); 

     call.enqueue(new Callback<Example>() { 

      @Override 
      public void onResponse(Response<Example> response, Retrofit retrofit) { 

       try { 
        mMap.clear(); 
        // This loop will go through all the results and add marker on each location. 
        for (int i = 0; i < response.body().getResults().size(); i++) { 
         Double lat = response.body().getResults().get(i).getGeometry().getLocation().getLat(); 
         Double lng = response.body().getResults().get(i).getGeometry().getLocation().getLng(); 
         String placeName = response.body().getResults().get(i).getName(); 
         String vicinity = response.body().getResults().get(i).getVicinity(); 
         MarkerOptions markerOptions = new MarkerOptions(); 
         LatLng latLng = new LatLng(lat, lng); 
         // Position of Marker on Map 
         markerOptions.position(latLng); 
         // Adding Title to the Marker 
         markerOptions.title(placeName + " : " + vicinity); 
         // Adding Marker to the Camera. 
         Marker m = mMap.addMarker(markerOptions); 
         // Adding colour to the marker 
         markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)); 
         // move map camera 
         mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
         mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 
        } 
       } catch (Exception e) { 
        Log.d("onResponse", "There is an error"); 
        e.printStackTrace(); 
       } 
      } 
      @Override 
      public void onFailure(Throwable t) { 
       Log.d("onFailure", t.toString()); 
      } 
     }); 

    } 

    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000); 
     mLocationRequest.setFastestInterval(1000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     Log.d("onLocationChanged", "entered"); 

     mLastLocation = location; 
     if (mCurrLocationMarker != null) { 
      mCurrLocationMarker.remove(); 
     } 
     //Place current location marker 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title("Current Position"); 

     // Adding colour to the marker 
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 

     // Adding Marker to the Map 
     mCurrLocationMarker = mMap.addMarker(markerOptions); 

     //move map camera 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 

     Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f", latitude, longitude)); 

     Log.d("onLocationChanged", "Exit"); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 
    public boolean checkLocationPermission(){ 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 

      // Asking user if explanation is needed 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.ACCESS_FINE_LOCATION)) { 

       // Show an explanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 

       //Prompt the user once explanation has been shown 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSIONS_REQUEST_LOCATION); 


      } else { 
       // No explanation needed, we can request the permission. 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSIONS_REQUEST_LOCATION); 
      } 
      return false; 
     } else { 
      return true; 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case MY_PERMISSIONS_REQUEST_LOCATION: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted. Do the 
        // contacts-related task you need to do. 
        if (ContextCompat.checkSelfPermission(this, 
          Manifest.permission.ACCESS_FINE_LOCATION) 
          == PackageManager.PERMISSION_GRANTED) { 

         if (mGoogleApiClient == null) { 
          buildGoogleApiClient(); 
         } 
         mMap.setMyLocationEnabled(true); 
        } 

       } else { 

        // Permission denied, Disable the functionality that depends on this permission. 
        Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show(); 
       } 
       return; 
      } 

      // other 'case' lines to check for other permissions this app might request. 
      // You can add here other case statements according to your requirement. 
     } 
    } 
} 

ответ

2

The signature of onResponse отличается от того, что у вас есть.

У вас есть:

public void onResponse(Response<Example> response, Retrofit retrofit) 

Оно должно быть:

public void onResponse(Call<Example> call, Response<Example> response) 

Ваш onFailure также должен принять Call параметр:

public void onFailure(Call<Example> call, Throwable t) 

Возможно подписи методов есть изменено между версиями Retrofit, и что учебник используя более старую версию, чем вы. Но я только размышляю.

+1

Это сработало! Спасибо! –

1

Это очень легко диагностировать: Просто прочитайте сообщение об ошибке и понять:

It говорит, что вы пытаетесь подключить интерфейс CallBack, поэтому вам придется переопределить все методы, которые он объявляет. Но вы не; потому что вы не переопределили метод onFailure(Call<T> call, Throwable t).

См.? Вы пропустили добавление параметра call в методе onFailure.

Не доверяйте так сильно на уроки; используйте его в качестве основы, а не как Евангелие. Возможно, этот учебник основан на предыдущей версии Retrofit. (Или это может содержать ошибки).

+0

[Godspell] (https://en.wikipedia.org/wiki/Godspell)? Я думаю, вы имеете в виду евангелие – Michael

+0

Спасибо. Я буду более осторожен с учебниками. –

+1

@ Майкл справа. Спасибо. –

 Смежные вопросы

  • Нет связанных вопросов^_^