Android Google地图:在活动

时间:2016-08-24 06:34:44

标签: android google-maps fragment

您好我试图将GoogleMap片段添加到我的活动中

这是我的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>

我的LocateusActivity onCreate方法

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

不知道出了什么问题。我收到了这个错误

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

任何帮助都非常感谢。谢谢!

您好我尝试将MapFragment替换为SupportMapFragment但错误相同并与此

一起使用
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" /> 

1 个答案:

答案 0 :(得分:1)

根据official documentation

XML布局文件

默认情况下,定义应用程序布局的XML文件位于res/layout/activity_maps.xml。它包含以下内容:

<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" />

地图活动Java文件

默认情况下,定义maps活动的Java文件名为MapsActivity.java。它应该在您的包名后包含以下代码:

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

如果您的地图活动文件不包含上述代码,请在您的包名后面用上面的代码替换该文件的内容。