Switch between Fragments and MapFragment with NavigationDrawer *FIXED*

落爺英雄遲暮 提交于 2019-12-01 11:54:21

问题


Problem Solved. Scroll further down!

Im currently coding an app that has a Navigation Drawer. I want a map in one of the fragments.

To switch between fragments in the navigation drawer i have this Switch case:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;


    switch (position) {
    case 0:


        fragment =  new SupportMapFragment();

        break;
    case 1:
        fragment = new PlacesFragment();

        break;
    case 2:
    fragment = new LogbookFragment();

        break;
    case 3:
    fragment = new SettingsFragment();

        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();



        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

and this is my fragment with the map:

public class MapFragment extends SupportMapFragment {

private GoogleMap map;

public MapFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_map, null, false);

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();
    return v;
}

}

The problem is that i can't switch to the mapfragment. Iv tried to modify the Swich case but i can't get it to work. For an example, i can't get the method replace() to work with SupportMapFragment.

Can anyone please help me with my problem?

Thank you.

EDIT: Updated the code. Still doesnt work.

Error at case 0: "Type mismatch: cannot convert from SupportMapFragment to Fragment".

EDIT 2: Problem solved! The biggest error was that i imported wrong fragment. The correct import is android.support.v4.app.Fragment and android.support.v4.app.FragmentManager. When i fixed that, I had to change some stuff in my Main activity where i have my Navigation Drawer.

After that were fixed, the map worked, but when i switched fragment via the Navigation Drawer and back, the app crashed. I fixed that with a bit of a work around. Im posting the updated code below this.

This is where the content updates and the fragments replace each other.

private void displayView(int position) {
    // update the main content by replacing fragments
    android.support.v4.app.Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new PlacesFragment();

        break;
    case 1:
        fragment = new MapFragment();

        break;
    case 2:
        fragment = new LogbookFragment();

        break;
    case 3:
        fragment = new SettingsFragment();

        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

And this is the fragment with the map.

public class MapFragment extends Fragment {

private GoogleMap map;

public MapFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_map, null, false);

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();

    return v;
}

@Override
public void onDestroyView() {

    SupportMapFragment f = (SupportMapFragment) getFragmentManager()
            .findFragmentById(R.id.map);

    if (f != null) {
        try {
            getFragmentManager().beginTransaction().remove(f).commit();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    super.onDestroyView();
}

}

The method "onDestroyView" is what fixed my crashing problem.


回答1:


Try this for case 0:

 fragment = new MapFragment(); 

since you are extending SupportMapFragment (which extends fragment)



来源:https://stackoverflow.com/questions/22478475/switch-between-fragments-and-mapfragment-with-navigationdrawer-fixed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!