我想从标签页面适配器调用地图。 pageradapter放入主片段。 当调用getItem()时,我的应用程序崩溃
public static class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentTest();
break;
case 1:
fragment = new FragmentMap(); //here's error!!
break;
}
return fragment;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
地图片段
public class FragmentMap extends Fragment{
Context context;
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.people_map_fragment_layout, container, false);
// Gets the MapView from the XML layout and creates it
context = getActivity().getApplicationContext();
try {
MapsInitializer.initialize(context);
} catch (GooglePlayServicesNotAvailableException e) {
Log.e("Address Map", "Could not initialize google play", e);
}
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) )
{
case ConnectionResult.SUCCESS:
Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView) v.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if(mapView!=null)
{
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
}
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default: Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
return v;
}
@Override
public void onResume() {
super.onResume();
if(mapView!=null)
mapView.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
if(mapView!=null)
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
if(mapView!=null)
mapView.onLowMemory();
}
}
但我总是错误:
java.lang.VerifyError:FragmentMap
at FragmentMain$PagerAdapter.getItem()
我的代码出了什么问题?请帮忙
答案 0 :(得分:0)
当我覆盖我不应该覆盖的方法时,我发现这种情况发生了,在你的情况下,我会检查是否允许覆盖方法getItemPosition和getPageTitle,因为,来自FragmentPagerAdapter文档,(http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html), 子类只需要实现getItem(int)和getCount()就可以有一个工作适配器。该文档还说,当使用FragmentPagerAdapter时,主机ViewPager必须具有有效的ID集,因此这将是另一件要验证的事情。