Thomas Dang
Thomas Dang

Reputation: 231

Google Map Nested Fragment does not show up

so I am currently doing a navigation application where I need to display several tabs, one of which show a google map.

I have used FragmentPagerAdapter to set up the multi-pages UI and so far for the 2 remaining fragments which do not implements any other nested fragment, it works just fine.

For the map fragment, I noticed the problem of nested fragment, so I followed this guide: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1 and put the things accordingly into my MapFragment, however, the Fragment does not show me anything, it is just a blank page (I have some other things in that fragment, not just the google map fragment).

Please see where I made mistake, thank you very much :) If you need any other part of the code please tell me.

Here is the code of the Map Fragment

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);
    }
}

And here is the map.xml file

<?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>

How it is supposed to look like (inside Eclipse View):

enter image description here

How it is on my phone:

enter image description here

Update: After some times looking through the internet, I suspects that the problem comes from the inflating part of the code (if not at least it should show something). May be to in inflate a Relative layout, you need to have some special code than when you inflate a Linear Layout. All the codes I found is for Activity inflation, if anyone know how to inflate a Relative Layout in Fragment, please help. Thank you :)

Upvotes: 0

Views: 883

Answers (2)

Thomas Dang
Thomas Dang

Reputation: 231

Update: My problem comes from the most unexpected place, I declare a getView() function in the MapFragment which seems to be overriding a function in Fragment class and everything get wrong from that part. Just delete the function out and it will work like a champ.

This do shows me that error can come from place where you dont expect (What I do is to comment the whole thing out then test block by block till I find the part that makes my program goes wrong). Thank you very much for others to reply to me. Very appreciate your help :)

Upvotes: 0

orium
orium

Reputation: 3813

from android developers :

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.

So, instead of <fragment> element use some container view (FrameLayout for example) for adding fragment dynamicly

Upvotes: 1

Related Questions