Navigation Drawer + Google Maps

0

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.

    
asked by anonymous 17.03.2016 / 06:59

1 answer

0

From what I understood the question you made it work.

  

With value android.support.v4.app.Fragmentem MainFragment the MainActivity does not give error

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

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

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

You now have to know if your app will work in versions smaller than API11 if you do modify your MainFragment class to inherit from android.support.v4.app.Fragment and ready, otherwise use as the default android.app.Fragment and your MainAcvitity change the getSupportFragmentManager because you will not support the lower API11 version for.

MainFragment fragment = new MainFragment();
FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
transaction1.replace(R.id.prefsfragment, fragment);
transaction1.commit();
    
17.03.2016 / 22:02