2017-08-29 107 views
0

我使用本指南: https://developers.google.com/maps/documentation/android-api/Android的谷歌地圖碎片錯誤

但它不工作,我已經註冊在谷歌控制檯的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 
+0

問題是在XML文件中。請加入這行,並嘗試<?XML版本=「1.0」編碼=「UTF-8」> –

+0

@JRamesh我加入這一行,但不能正常工作,它沒有解決我的問題 – aimprogman

回答

0

我很抱歉,我錯過了添加

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

它做工精細,感謝

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

這也是初始化的方法地圖,它在所有設備上效果更好。

如果你仍然想使用的地圖碎片您的地圖請出示你的清單,所以我可以看到,如果你已經錯過了一些東西在那裏。

+0

感謝我錯過<使用許可權的android:名稱= 「android.permission.ACCESS_NETWORK_STATE」/> – aimprogman