Google在抽屉片段内部映射,找不到ID为XXXXXXX的视图

时间:2016-06-27 09:25:34

标签: java android google-maps android-fragments

  

错误:致命异常:主程序:groep2.project4,PID:18507   java.lang.IllegalArgumentException:找不到id 0x7f0d00a5的视图   (groep2.project4:id / map)片段SupportMapFragment {b36c267#0   ID = 0x7f0d00a5}

DrawerActivity.java

} else if (id == R.id.locatie) { fragment = fragLocatie;
    if(!sMapFragment.isAdded()) {
        sFragmentManager.beginTransaction().add(R.id.map, sMapFragment).commit();
    }else{sFragmentManager.beginTransaction().show(sMapFragment).commit();}

Locatie.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <FrameLayout
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>

    </LinearLayout>
</RelativeLayout>

content_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/drawer_start"
    tools:context="groep2.project4.DrawerActivity"
    tools:showIn="@layout/app_bar_drawer"
    android:background="#7EC580"
    android:orientation="vertical">




    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="23">

    </FrameLayout>

</LinearLayout>

纠正我,如果我错了,但我认为这是因为地图不是抽屉活动的孩子,但我想在“locatie”片段中显示地图。

任何人都可以在这里指导我正确的方向吗?将地图移动到content_drawer.xml使其在所有片段中都可见,我完全不知道如何解决这个问题

编辑:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        sMapFragment = SupportMapFragment.newInstance();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
//        navigationView.setItemTextColor(ColorStateList.valueOf(Color.DKGRAY));

        menucolormanager.HeadingCharts(navigationView.getMenu().findItem(R.id.categorie1),navigationView.getMenu().findItem(R.id.categorie2),navigationView.getMenu().findItem(R.id.categorie3), navigationView.getMenu().findItem(R.id.categorie4));

        sMapFragment.getMapAsync(this);
    }

    public boolean onNavigationItemSelected(MenuItem item) {

        android.support.v4.app.FragmentManager sFragmentManager = getSupportFragmentManager();


        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment fragment;

        if(sMapFragment.isAdded()){
            sFragmentManager.beginTransaction().hide(sMapFragment).commit();
        }

        if (id == R.id.fietstrommels) {fragment = fragFietsTrommels;

        } else if (id == R.id.diefstallen) { fragment = fragDiefstallen;

        } else if (id == R.id.locatie) { fragment = fragLocatie;
            if(!sMapFragment.isAdded()) {
                sFragmentManager.beginTransaction().add(R.id.map, sMapFragment).commit();
            }else{sFragmentManager.beginTransaction().show(sMapFragment).commit();}

        } else if (id == R.id.route) { fragment = fragRoute;

        } else if (id == R.id.kladblok) { fragment = fragKladblok;

        } else {return false;}

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.drawer_start, fragment).commit();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

        return true;
    }

1 个答案:

答案 0 :(得分:1)

Locatie fragment类中添加SupportMapFragment时,您必须使用ChildFragmentManager作为fragmentManager,因为您要在片段内添加片段。以下是示例代码:

FragmentManager sFragmentManager = getChildFragmentManager();
sFragmentManager.beginTransaction().replace(R.id.map, sMapFragment).commit()

希望它有效,如果你在实施同样的问题时告诉我。