0

我正在尝试在我的 DrawerLayout 项目之一中使用 MapFragment。

我的抽屉布局:

private void selectItem(int position) {
        switch (position) {
            (...)
            case 3:
                fragment = new MyMapFragment();
                break;
            (...)
            //THIS CODE WORKS WITH ANY OTHER FRAGMENT I CREATED!!!!!!
        }
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commit();

此代码适用于我所有的 Fragment,并且永远不会崩溃,除了 MyMapFragment。这个(只有这个)在设备旋转时崩溃,而代码与我的其他片段非常相似。

唯一的区别是我使用的是嵌套片段:

public class MyMapFragment extends Fragment {
 public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        mMapFragment = new MapFragment();

        GoogleMap map = mMapFragment.getMap();

        FragmentTransaction fragmentTransaction =
            getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.container, mMapFragment);
        fragmentTransaction.commit();
}

我不知道那里有什么问题...

日志猫:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.*/com.*.MainActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.*.MyMapFragment$1: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
4

1 回答 1

1

扩展FragmentActivity而不是MapFragement

现在使用SupportMapFragment

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

map_layout.xml

<fragment
            android:id="@+id/map"

            android:name="com.google.android.gms.maps.SupportMapFragment" 

            android:layout_width="match_parent"
            android:layout_height="match_parent" />
于 2013-06-19T13:23:19.120 回答