I have a problem joining Navigation Drawer like Google Maps. I want google to be my initial fragment and I want it to be in the Navigation Drawer list as an item, but there is a small catch. I will try to explain it in the best way and I hope you will help me.
In the Class of MainFragment
(Maps Code) it asks me to import android.app.Fragment
but when I go to MainActivity
it asks that in class MainFragment
be imported android.support.v4.app.Fragment
Main Fragment
public class MainFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container,false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(-33.867, 151.206);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 13));
googleMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(marker));
}
With android.app.Fragment
does not have any errors, but when I import android.support.v4.app.Fragment
the error happens in (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set the fragment initially
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
With amount android.support.v4.app.Fragment
in MainFragment
a MainActivity
does not give error, but does matter android.app.Fragment
error happens in (where it is bold):
fragmentTransaction.replace (R.id.fragment_container, fragment );
Basically on each side they ask for different amounts, I know where the error is, but I do not know how to fix it.