J.Tey J.Tey - 1 year ago 160
Java Question

Google Maps: Create marker from different class

In my android studio project, I have two activites: one MapsActivity and the other just basic.

When I click on the map, the other activity launches which consists of a set of EditTexts (textfields) allowing user to define custom properties for the marker. However, when I try to create the marker from that class, Android Studio gives me an error.

Here is the code, MapsActivity.java:

package com.geekybrackets.virtualtourguide;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

public GoogleMap mMap;
double lat = 0;
double lon = 0;

protected void onCreate(Bundle savedInstanceState) {
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()


* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

public void onMapClick(LatLng latLng) {

lat = latLng.latitude;
lon = latLng.longitude;
startActivity(new Intent(MapsActivity.this, newMarker.class));




NewMarker.java (activity where user enters properties of marker)

package com.geekybrackets.virtualtourguide;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class newMarker extends AppCompatActivity{
public void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

Button save_btn = (Button)findViewById(R.id.btn_save);
save_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

EditText editName = (EditText)findViewById(R.id.editName);
String marker_title = editName.getText().toString();
MapsActivity i = new MapsActivity();
i.mMap.addMarker(new MarkerOptions()
.position(new LatLng(i.lat, i.lon))
i.mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(i.lat, i.lon)));


This is the error i get:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
at com.geekybrackets.virtualtourguide.newMarker$2.onClick(newMarker.java:42)

How could i solve this issue? Any input wil be appreciated :)

Answer Source

Oh boy, you've got a couple of things to learn.

First, you should never invoke an Activity's constructor - the idea is that the framework creates an instance of your Activity and you handle initialization logic on onCreate(Bundle savedInstanceState). If you just call the constructor, you create a random object which is not even going to be part of your UI, let alone the instance if MapsActivity with which you want to work.

Second, newMarker is a horrible class name, you should keep with the Java convention and start class names with an upper case letter, something like NewMarkerActivity,

Third, if you want to update MapsActivity from another Activity, you need to start the second activity for a result from the first activity. Basically, you should call startActivityForResult(new Intent(MapsActivity.this, NewMarkerActivity.class)) instead of your current call, and override onActivityResult in MapsActivity. Pass the title as an extra in the result Bundle and retrieve it in onActivityResult.

Consult the documentation for getting results

Edit: To pass the title, you need to create an Intent and pass in the title with a call to Intent.putExtra("someKey", theTitle). Afterwards, call Activity.setResult(int, Intent) with that Intent. In onActivityResult, you can get the title by calling getStringExtra("someKey")

It's a good idea to make that key a public static final field of your NewMarkerActivity class, so that you don't make a typo.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download