-2

Hi I'm trying to add GoogleMap fragment into my activity

Here is my XML:

  <LinearLayout
        android:id="@+id/layout_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/list_margin"
        android:visibility="visible" >

        <fragment
            android:id="@+id/map_frame"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

And my LocateusActivity onCreate method

mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map_frame);

Don't know what went wrong. I got this error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.singPost/com.singPost.LocateUsActivity}: android.view.InflateException: Binary XML file line #80: Binary XML file line #80: Error inflating class fragment
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                                                                at android.app.ActivityThread.startActivityNow(ActivityThread.java:2977)

enter image description here

Any help is much appreciate. Thanks!

Hi I try replace MapFragment to SupportMapFragment but same error and together with this

Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 9452000 but found 6587000. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77

1 Answers1

2

As per the official documentation:

The XML layout file

By default, the XML file that defines the app's layout is at res/layout/activity_maps.xml. It contains the following :

<fragment 
    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"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

The maps activity Java file

By default, the Java file that defines the maps activity is named MapsActivity.java. It should contain the following code after your package name:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

If your maps activity file doesn't contain the above code, replace the contents of the file with the above code, after your package name.

G. Spyridakis
  • 431
  • 1
  • 10
  • 21