I am trying to create an app with a simple UI. It consists of one activity and which comprises of 2 buttons, a textview and a google maps fragment which fills up half the screen. I've created the layout using drag and drop elements provided from Android Studio.
The problem is that when I try to run it, I get the following error:
jjava.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
This is my activity:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{
RequestQueue requestQueue;
Button serviceButton;
TextView textViewService;
SensorResponse responseJson;
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestQueue = Volley.newRequestQueue(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView2);
mapFragment.getMapAsync(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
textViewService = findViewById(R.id.textViewServiceOutput);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void setUpMap(){
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}}
I've googled the error and have tried the solutions that I came across such as trying to SupportMapFragment
but it still does not work.
Following is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.tarikh.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="callTheService"
android:text="Call Service"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mapButton" />
<TextView
android:id="@+id/textViewServiceOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="128dp"
android:layout_marginRight="128dp"
android:layout_marginTop="16dp"
android:text="Parking Bay Checker"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/mapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="callTheMap"
android:text="Call Map"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewServiceOutput" />
<com.google.android.gms.maps.MapView
android:id="@+id/mapView2"
android:layout_width="match_parent"
android:layout_height="244dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="44dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
</android.support.constraint.ConstraintLayout>
EDIT: Thanks for the input, after a bit a tweaking, I've managed to get it working. I replaced the map on my activity layout with the following:
<fragment
android:id="@+id/mapFrag"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="329dp"
android:layout_marginTop="164dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />