【发布时间】:2014-07-21 05:35:07
【问题描述】:
所以我目前正在做一个导航应用程序,我需要在其中显示几个选项卡,其中一个显示谷歌地图。
我已经使用 FragmentPagerAdapter 来设置多页 UI,到目前为止,剩下的 2 个片段没有实现任何其他嵌套片段,它工作得很好。
对于地图片段,我注意到了嵌套片段的问题,所以我按照这个指南:https://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1 并将相应的东西放入我的 MapFragment 中,但是,片段没有显示任何东西,它只是一个空白页面(我在那个片段中还有一些其他的东西,而不仅仅是谷歌地图片段)。
请看我哪里出错了,非常感谢:)如果您需要代码的任何其他部分,请告诉我。
这是地图片段的代码
public class MapFragment extends FragmentControl implements OnSeekBarChangeListener{
public static MapFragment getInstance() {
if(MapFragment.instance==null) {
Log.e(TAG,"Map Fragment is not loaded");
}
return MapFragment.instance;
}
public static MapFragment newInstance(int position, String title){
MapFragment mapFragment = new MapFragment();
Bundle args = new Bundle();
args.putInt("current_page", 1);
args.putString("page_tile", "Map Information");
mapFragment.setArguments(args);
return mapFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCurrentPage = getArguments().getInt("current_page", 1);
pageTitle = getArguments().getString("page_title");
MapFragment.instance=this;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
layout = inflater.inflate(R.layout.map, container, false);
if (layout==null){
Log.d(TAG,"layout null");
return null;
}
CheckBox camera = (CheckBox)layout.findViewById(R.id.cameraMove);
camera.setChecked(MapFragment.moveCamera);
camera.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
MapFragment.moveCamera = isChecked;
}
});;
mTransparencyBar = (SeekBar)layout.findViewById(R.id.transparencySeekBar);
mTransparencyBar.setMax(TRANSPARENCY_MAX);
mTransparencyBar.setProgress(0);
markerList = new ArrayList<Marker>();
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
supportfragment = (SupportMapFragment) fm.findFragmentById(R.id.googlemap);
if (supportfragment == null) {
supportfragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.googlemap, supportfragment).commit();
fm.executePendingTransactions();
}
}
@Override
public void onResume() {
super.onResume();
this.createUiMap();
mapTimer = new Timer();
mapTimer.scheduleAtFixedRate(new updateCalculationTask(), 0, 100);
mapTimer.scheduleAtFixedRate(new updateUITask(), 50, MainActivity.uiUpdateRate);
}
void createUiMap() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = supportfragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
populateMapStartPointsSpinner(layout);
setStartPointSpinnerListener(layout);
this.setUpMarker(this.defaultStartPoint.x, this.defaultStartPoint.y);
FetchSQL.setDRFixData(this.setLocation(defaultStartPoint));
}
}
}
private void setUpMap() {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 20));
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.i3f2));
mCurrentEntry = 0;
LatLngBounds newarkBounds = new LatLngBounds(
new LatLng(1.291926, 103.775114), // South west corner
new LatLng(1.292833, 103.776310)); // North east corner
mGroundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
.image(mImages.get(mCurrentEntry))
.positionFromBounds(newarkBounds));
mTransparencyBar.setOnSeekBarChangeListener(this);
}
}
这是 map.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/cameraMove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cameraMove"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/mapStartPointSpinnerLabel"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cameraMove"
android:layout_toRightOf="@id/cameraMove"
android:text="@string/mapStartPointSpinnerLabel" />
<Spinner
android:id="@+id/mapStartPointSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/cameraMove"
android:layout_toRightOf="@id/mapStartPointSpinnerLabel" />
<LinearLayout
android:id="@+id/seekbarmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/mapStartPointSpinnerLabel"
android:orientation="horizontal"
android:layout_marginTop="15dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/transparency"
android:layout_gravity="center_vertical" />
<SeekBar
android:id="@+id/transparencySeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/seekbarmenu"
android:layout_marginTop="15dip" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dip"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/googlemap" />
</LinearLayout>
它应该是什么样子(在 Eclipse 视图中):
我手机上的情况:
更新:在网上浏览了一段时间后,我怀疑问题来自代码的膨胀部分(如果不是至少它应该显示一些东西)。可能是在inflate a Relative layout,你需要一些特殊的代码而不是inflate Linear Layout。 我找到的所有代码都是针对 Activity 膨胀的,如果有人知道如何在 Fragment 中膨胀相对布局,请提供帮助。谢谢你:)
【问题讨论】:
-
试试这里,也许可以帮助你解决这个错误stackoverflow.com/questions/18206615/…
标签: android google-maps android-layout android-fragments android-nested-fragment