2017-06-14 127 views
0

我一直在嘗試使用谷歌地圖片段。該項目工作正常,沒有我試圖爲每個標記添加的自定義信息窗口,但是當我添加信息窗口時,我得到了一個N​​ullPointerException。如下所示:java.lang.RuntimeException:無法在Google地圖上啓動活動ComponentInfo活動

FATAL EXCEPTION: main 
Process: com.couchbase.grocerysync, PID: 6820 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.couchbase.grocerysync/com.couchbase.grocerysync.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference 

下面是佈局文件:

<com.couchbase.grocerysync.MapWrapperLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/map_relative_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<AutoCompleteTextView 
android:id="@+id/autoCompleteTextView" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:ems="10" 
android:layout_weight="1.06" 
android:text="Please enter your place" > 
<requestFocus /> 
</AutoCompleteTextView> 


<Button 
android:id="@+id/Bsearch" 
style="?android:attr/buttonStyleSmall" 
android:layout_width="90dp" 
android:layout_height="wrap_content" 
android:layout_gravity="right" 
android:onClick="onSearch" 
android:text="Search" /> 
</LinearLayout> 

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

這是setUpMapIfNeeded()方法,從的onCreate和其中出現錯誤稱爲:

private void setUpMapIfNeeded() { 
     if (mMap == null) { 
     AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); 

     autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item)); 
     autoCompView.setOnItemClickListener(this); 
     // Try to obtain the map from the SupportMapFragment. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); //here is the error! 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 

任何幫助將不勝感激!

+0

你可以發佈你的java代碼嗎? –

+0

我剛纔編輯了我的問題 –

回答

0

mapFragment返回null。在你的xml中,你正在使用MapFrgament,而在Java中,你正在給SupportMapFragment充氣。 在XML

class="com.google.android.gms.maps.MapFragment". 

替換該行與

android:name="com.google.android.gms.maps.SupportMapFragment". 

應該工作。

相關問題