I am trying to put a map with a marker in an activity in an Android app. It compiles and builds fine, but when I try to run the activity the app crashes and I get the message "Unfortunately, AppName has stopped"
The log from Android Studio gives me the following message:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method >'void >com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.androi>d.gms.maps.OnMapReadyCallback)' on a null object reference.
Any ideas what's causing this? This same code has worked fine in other apps. Why is the parameter 'this' considered a null object reference when I call the method getMapAsynch()? I've also tried DayCenter.this with the same results.
Any assistance 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's the layout file. I tried changing "com.google.android.gms.maps.MapFragment" to SupportMapFragment, but it still doesn't 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"/>