将Google地图片段嵌套在自定义片段中

3
我想创建一个包含碎片的滑动视图。其中一个碎片应该包含GoogleMap MapFragment。在主活动中直接填充MapFragment可以正常工作。但是通过FragmentPagerAdapter进行操作时,会在Choreographer.class中抛出源未找到的异常。我无法找到原因。我根据this的答案设置了所有内容。所以我的代码看起来像这样:

MainActivity

public class MainActivity extends FragmentActivity {

FragPagerAdapter _fragPagerAdapter;
ViewPager _viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    _fragPagerAdapter = new FragPagerAdapter(getFragmentManager());
    _viewPager = (ViewPager)findViewById(R.id.pager);
    _viewPager.setAdapter(_fragPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

public class FragPagerAdapter extends  FragmentPagerAdapter {

    public FragPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;

        if(position == 1){
        fragment = new LocMapFragment(); //CRASHES 
        }else{
            //Doesn't contain map .WORKS
            fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
        }

        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:

            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}


}

LocMapFragment

public class LocMapFragment extends Fragment {

private GoogleMap  _map;
@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState){

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

  _map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
      .getMap();

  return v;
   }
}

map_layout布局

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

    <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

 </RelativeLayout>

我正在使用Android API中的Fragment而不是支持V4。我尝试将片段布局更改为class="com.google.android.gms.maps.SupportMapFragment",但仍然相同。
更新: 好的,不知何故现在它开始工作了。但现在发生的是,如果地图片段在第一个滑动片段中,然后我还有两个只有文本的滑动。如果我从地图滚动到另一个位置,然后再滚回来,我会在Choreographer.class上得到异常。 这个答案似乎解决了问题。
1个回答

1

看一下这个实现,你应该已经做过了,创建一个包含地图的片段:

public class MapFragment extends Fragment

创建一个布局: mapview.xml:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="12"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />

然后在片段中执行以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.mapview, container, false);
}

然后获取地图实例:
private GoogleMap getGoogleMap() {
        if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) {
            SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview);
            if (smf != null) {
                map = smf.getMap();
            }
        }
        return map;
    }

好的,现在它开始工作了。不知道为什么。但是现在我有一个关于滑动的问题。请看我的更新。 - Michael IV
无法检测。但似乎它会重新创建那些滚动回来的超出视图范围的片段。 - Michael IV
我实现了这个解决方案,尽管最初成功了,但由于嵌套片段不受Android支持,有时会导致我的应用程序崩溃,并出现“重复的id、标记为空或父id为0x0与com.google.android.gms.maps.SupportMapFragment的另一个片段”的异常。因此,我改变了我的方法,按照以下方式进行:https://dev59.com/kWYr5IYBdhLWcg3wXJEK#13734470,简单地扩展SupportMapFragment并使用onCreateView()获取我需要的所有内容。现在一切都像魅力一样工作! - javaxian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接