我正在尝试使用片段创建滑动视图。其中一个片段应该包装 GoogleMap MapFragment。直接在主活动中膨胀 MapFragment 可以。但是通过 FragmentPagerAdapter 会抛出在 Choreographer.class 中找不到的源。我找不到任何线索。发生了。我把这一切都设置基于this https://stackoverflow.com/questions/16978190/add-google-maps-api-v2-in-a-fragment回答。所以我的代码如下所示:
主要活动
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;
}
}
}
LocMap片段
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 中的片段,而不是支持 V4 中的片段。我尝试将片段布局更改为 class="com.google.android.gms.maps.SupportMapFragment",但仍然相同。
UPDATE:
好吧,不知怎的,它现在开始工作了。但是现在发生的情况是,如果地图片段位于第一个滑动片段中,然后我再滑动两次,只有文本。如果我从地图滚动到另一个地图,然后向后滚动,我是Choreographer.class 出现异常。
这个答案似乎已经解决了这个问题。 https://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi