片段和MapFragment与NavigationDrawer之间切换*固定* [英] Switch between Fragments and MapFragment with NavigationDrawer *FIXED*

查看:234
本文介绍了片段和MapFragment与NavigationDrawer之间切换*固定*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题解决了。滚动再往下!

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;
}

}

现在的问题是,我不能切换到mapfragment。四,试图修改SWICH情况下,但我不能让它的工作。举个例子,我不能让replace()方法与SupportMapFragment工作。

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?

感谢你。

编辑:更新了code。还是不工作。

Updated the code. Still doesnt work.

目前情况下0错误:类型不匹配:不能从SupportMapFragment转换为分段。

修改2:问题解决的最大的错误是,我错了进口的片段。正确的导入 android.support.v4.app.Fragment android.support.v4.app.FragmentManager 。当我固定的,我不得不改变一些东西在我的主要活动在那里我有我的抽屉式导航。

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.

在这之后是固定的,地图上的工作,但是当我打开片段通过导航抽屉和背部,应用程序崩溃。我定了一个位工作的周围。林张贴低于此更新code。

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.

公共类MapFragment扩展片段{

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();
}

}

方法onDestroyView是固定我崩溃的问题。

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

推荐答案

尝试此情况下0:

 fragment = new MapFragment(); 

因为你正在扩展SupportMapFragment(延伸片段)

since you are extending SupportMapFragment (which extends fragment)

这篇关于片段和MapFragment与NavigationDrawer之间切换*固定*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆