0

我是新來的Android,並試圖讓我的應用程序打開一個新的活動按鈕點擊顯示使用谷歌地圖Android API的地圖。Android,谷歌地圖Android的API,設置位置和地圖類型

我可以得到一個標準的地圖渲染按鈕點擊沒有問題,但是當我嘗試添加一個位置和一個地圖類型,我的應用程序崩潰與此錯誤。

「null:java.lang.NullPointerException:試圖調用虛方法com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()'null對象引用「

下面是我的Activity指向的xml片段佈局。

<?xml version="1.0" encoding="utf-8"?> 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:name="com.google.android.gms.maps.MapFragment" 
    /> 

這是我的java代碼。

map1 = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1)).getMap(); 
     if (map1 == null) 
      Toast.makeText(this, "No Maps", Toast.LENGTH_LONG).show(); 
     else 
      map1.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
      final LatLng LOCATION_1 = new LatLng(37.7576171,-122.5776844); 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(LOCATION_1) 
       .zoom(17) 
       .bearing(90) 
       .tilt(30) 
       .build(); 
     map1.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

我已經做了幾個小時的研究,而且我很茫然。我想知道在我的清單中是否有可能丟失了某些東西?

任何幫助,將不勝感激。

我的清單以防萬一。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="...." 
    xmlns:tools="http://schemas.android.com/tools"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Activity2"> 
     </activity> 
     <activity 
      android:name=".Activity3"> 
     </activity> 


     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="8298000" 
      tools:replace="android:value"/> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyCCk5zREDbtWJD-tFru8xmRZow0ocVXIps"/> 

    </application> 


    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 


</manifest> 

回答

0

首先,改一下代碼,你在xml裏輸入MapFragment,你需要輸入SupportMapFragment。

代碼:

<?xml version="1.0" encoding="utf-8"?> 

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

其次,根據谷歌地圖文檔,的GetMap()方法被棄用。因爲它並不總是返回非空值。它可以爲空很多次: https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment.html#getMap()

使用getMapAsync()方法,此方法有一個回調機制。一旦地圖準備就緒,它會回撥。因此,它總是返回非空值。

完整代碼:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1); 
mapFragment.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 

     googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     final LatLng LOCATION_1 = new LatLng(37.7576171,-122.5776844); 
     CameraPosition cameraPosition = new CameraPosition.Builder() 
      .target(LOCATION_1) 
      .zoom(17) 
      .bearing(90) 
      .tilt(30) 
      .build(); 
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

    } 
}); 
+0

這種方法奏效。謝謝。 – Bubba