I am using places api and dependency
implementation 'com.google.android.libraries.places:places:1.0.0'
I want to add an AutocompleteSupportFragment in a fragment. Please help me where should I place the code in the fragment. I have tried on these methods
1. onCreate()
2. onCreateView()
3. onViewCreated()
4. onActivityCreated()
But it gives null object reference error. I have tried it with an activity, it works fine.
public class MyActivity extends Fragment implements OnMapReadyCallback{
AutocompleteSupportFragment autocompleteSupportFragment;
public ConsumerBookingMapFragment(){ }
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_consumer_booking_map,container,false);
return mView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null) {
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
mMapView = mView.findViewById(R.id.map);
if(mMapView != null){
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
This is a working solution.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
Use getChildFragmentManager() instead of getSupportFragmentManager()/getFragmentManager()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With