我使用操作栏标签和ViewPager
创建标签并在标签之间滑动。我使用SherlockFragmentActivity
作为标签页适配器。并为每个项扩展片段。这个项目适用于位置图页面(片段)中的所有页面,这是第一次有效,但是当我更改标签然后第二次返回时,位置图页面崩溃了。
错误行:查看
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_location_map, container, false);
return view;
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
tabAdapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new ViewSiteFragment();
case 1:
return new ViewSiteInfoFragment();
case 2:
return new LocationMapFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
答案 0 :(得分:0)
您的问题与您尝试在片段内膨胀片段有关。你应该扩展SupportMapFragment
而不是包装另一个片段。删除您的布局文件并删除onCreateView()
方法。然后将LocationMapFragment
扩展为SupportMapFragment
public class LocationMapFragment extends SupportMapFragment {
// code here without onCreateView
}
答案 1 :(得分:0)
谢谢你iagreen。我可以解决我的问题。
完整代码:
public class LocationMapFragment extends SupportMapFragment {
private LatLng mPosition;
public LocationMapFragment() {
super();
}
public static LocationMapFragment newInstance(LatLng posicion){
LatLng l=new LatLng(35.722838,51.371342);
LocationMapFragment frag = new LocationMapFragment();
frag.mPosition; = l;
return frag;
}
@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
View v = super.onCreateView(arg0, arg1, arg2);
initMap();
return v;
}
private void initMap(){
UiSettings settings = getMap().getUiSettings();
settings.setAllGesturesEnabled(false);
settings.setMyLocationButtonEnabled(false);
LatLng mPath=new LatLng(35.722838,51.371342);
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPath,16));
getMap().addMarker(new MarkerOptions().position(mPath).icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
getJson();
}
}