Navigation Drawer + Google Maps

Asked

Viewed 165 times

0

I have a problem joining Navigation Drawer as Google Maps. I want google to be my home page and I want it to be on the Navigation Drawer list as an item, but there’s been a little snag. I’ll try to explain it the best way and hope you help me.

In the Class of MainFragment(Google Maps Code) asks me to import android.app.Fragment but when I go to MainActivity 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 the android.app.Fragment there is no mistake, but when I care 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();

Import android.support.v4.app.Fragmentin MainFragment to MainActivity no mistake, but care android.app.Fragment the error occurs in (where it is in bold): fragmentTransaction.replace(R.id.fragment_container, Fragment);

Basically on each side they ask for different imports, I know I understand where the mistake is, but I don’t know how to fix it.

  • Lari, could you post the error stacktrace? This would help in solving your problem

  • Care android.support.v4.app.Fragment so much at MainFragment as in MainActivity doesn’t work?

1 answer

0

From what I understand the question you made work.

Import android.support.v4.app.Fragmentem Mainfragment to Mainactivity gives no error

but first of all you have to know the difference between these two Imports the (1)android.support.v4.app.Fragment and the (2)android.app.Fragment

1- is a class of support for Fragment created to give compatibility to versions smaller than API11

2- is a class of Fragment in native version, was introduced on Android3( API 11 ).

You now have to know if your app will work on versions smaller than API11 whether to modify its Mainfragment class to inherit from android.support.v4.app.Fragment and ready , otherwise use as the default android.app.Fragment and in its Mainacvitity replace getSupportFragmentManager for you nay will is supporting the smaller version API11 for.

MainFragment fragment = new MainFragment();
FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
transaction1.replace(R.id.prefsfragment, fragment);
transaction1.commit();
  • Hi Alessandro, could I get in touch with you ? I am new to programming on android and I have some doubts that I do not know with whom to take, If possible thank you very much, my email is [email protected].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.