Android谷歌地图片段错误

时间:2017-08-29 05:56:01

标签: android google-maps

我使用本指南: https://developers.google.com/maps/documentation/android-api/

但它不起作用,我在谷歌控制台注册了API密钥,并在清单中添加,我添加了清单权限。

这是MapsActivity:

package aimprogman.mustwork;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

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);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney 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));
  }
}

这是activity_maps.xml

  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

但它不起作用并显示错误:

08-29 10:51:01.767 15491-15491/aimprogman.mustwork E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: aimprogman.mustwork, PID: 15491
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{aimprogman.mustwork/aimprogman.mustwork.MapsActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
                                                                     at android.app.ActivityThread.access$900(ActivityThread.java:172)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:145)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                     at 

2 个答案:

答案 0 :(得分:0)

对不起,我错过了添加

      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

工作正常,谢谢

答案 1 :(得分:0)

在你的xml中使用下面的代码

 <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

在您的活动中,如下所示在OnCreate中启动 -

 mMapView = (MapView) rootView.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);
  try {
        MapsInitializer.initialize(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
 mMapView.getMapAsync(this);

这也是初始化地图的一种方式,它可以在所有设备上运行得更好。

如果你仍然想为你的地图使用地图片段,请告诉我你的清单,这样我就可以看到你错过了那里的东西。