我正在开发一个使用 Zomato Api 来获取附近餐馆的应用程序。我有这个片段向用户显示点击餐厅的详细信息。我的目标是在地图上显示餐厅的位置,但我在执行此操作时遇到了问题,因为我遇到了错误。它只在活动中有效吗?我是否必须将层次结构更改为 FragmentActivity?
我的片段
public class RestaurantDetails extends Fragment implements onMapReadyCallback{
private final String APIKEY="75be9f9e2239fe637bf9cb1b46979d91";
private SupportMapFragment mMapFragment;
private GoogleMap mGoogleMap;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mContentView = inflater.inflate(R.layout.restaurant_details, container,false);
int id= Integer.parseInt(getArguments().getString("id"));
mMapFragment=getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
getDetails(id);
return mContentView;
}
@Override
public void onMapReady(GoogleMap googleMap){
mGoogleMap=googleMap;
}
private void getDetails(int id) {
getApi().getRestaurantDetails(id, APIKEY)
.enqueue(new Callback<Restaurant_>() {
@Override
public void onResponse(Call<Restaurant_> call, Response<Restaurant_> response) {
addMarker( Double.parseDouble(response.body().getLocation().getLatitude()), Double.parseDouble(response.body().getLocation().getLongitude()),
response.body().getName());
}
@Override
public void onFailure(Call<Restaurant_> call, Throwable t) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Couldn´t load restaurant details");
AlertDialog mDialog = builder.create();
mDialog.show();
}
});
}
private void addMarker(double lat,double lon,String restaurantName){
LatLng latLng=new LatLng(lat,lon);
Marker marker=mGoogleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(restaurantName));
}
private Retrofit getRetrofit() {
return new Retrofit.Builder()
.baseUrl("https://developers.zomato.com/api/v2.1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
private ZomatoApi getApi() {
return getRetrofit().create(ZomatoApi.class);
}
}
我得到的错误是:
public class RestaurantDetails extends Fragment implements onMapReadyCallback
mMapFragment=getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);