Problem using getMapAsync in Google Maps

I am trying to put a map with a marker into action in an android application. It compiles and builds fine, but when I try to start the activity, the application crashes and I get the message "Unfortunately, AppName has stopped"

The log from Android Studio gives me the following message:

Called: java.lang.NullPointerException: trying to call the virtual method> 'void> com.google.android.gms.maps.SupportMapFragment.getMapAsync (com.google.androi> d.gms.maps.OnMapReadyCallback)' by reference to the null object.

Any ideas what causes this? The same code works fine in other applications. Why is the 'this' parameter considered a reference to a null object when I call the getMapAsynch () method? I also tried DayCenter.this with the same results.

Any help would be greatly appreciated.

package cetlot.com.sisuyouth; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.FragmentActivity; 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 DayCenter extends ActionBarActivity implements OnMapReadyCallback { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_day_center); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap map) { // Add a marker for the SISU Day Center, and move the camera. LatLng sisudaycenter = new LatLng(35.454439, -97.598672); map.addMarker(new MarkerOptions().position(sisudaycenter).title("SISU Day Center")); map.moveCamera(CameraUpdateFactory.newLatLng(sisudaycenter)); } } 

Here is the layout file. I tried changing "com.google.android.gms.maps.MapFragment" to SupportMapFragment, but it still does not work.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation = "vertical" tools:context="cetlot.com.sisuyouth.DayCenterFragment"> <TextView android:text="@string/day_center_address" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/day_center_address_line1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/day_center_address_line2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="@string/day_center_address_line3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.google.android.gms.maps.MapFragment" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> 

+4
source share
1 answer

Your xml uses MapFragment, not SupportMapFragment.

Modify your xml to use SupportMapFragment and it should work:

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

Also, make sure you inflate the correct layout when you call setContentView() .

+5
source

All Articles