Solution 1 :
Probably your mMapFragment
is null. Make sure it is not null. If it is maybe this link might help.
Problem :
I am devoloping an app that uses Zomato Api to get nearby restaurants. I have this fragment that shows the details of the clicked restaurant to the user. My goal is to show on a map the location of the restaurant but I am having trouble doing this since I am getting errors.Does it only work in an Activity? Do I have to change the hierarchy to FragmentActivity?
My Fragment
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);
}
}
The errors I get are in lines :
public class RestaurantDetails extends Fragment implements onMapReadyCallback
mMapFragment=getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
Comments
Comment posted by Tahir Ferli
There is a NullPointerException happening there. Your logs are pointing you where the error lays. mMapFragment is in RestaurantDetails fragment thats why it tells you which file to look. Once you handle NullPointerException the other errors are gonna be gone.
Comment posted by Tahir Ferli
If it is about implementing the interface just press CTRL + I and choose the interface you are supposed to override.