我一直在尝试使用谷歌地图片段。该项目工作正常,没有我试图为每个标记添加的自定义信息窗口,但是当我添加信息窗口时,我得到了NullPointerException。如下所示:
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 :(得分:0)
mapFragment返回null。在您的xml中,您使用的是MapFrgament,而在Java中,您正在使用SupportMapFragment。 在xml中替换此行
class="com.google.android.gms.maps.MapFragment".
带
android:name="com.google.android.gms.maps.SupportMapFragment".
应该工作。