1

I am a bit new to Android development, so please bear with me.

I am making a map of my campus using Google Maps API V2. Currently I am trying to set Markers on each of the building, and I succeeded in doing that. However, I decided to move the marker code to a separate class (to reduce clutter), but this is giving me problems.

Here is the relevant code fom my main class MapActivity.java

`public class MapActivity extends Activity implements OnMenuItemClickListener {

    private static final LatLng UCTLL = new LatLng(-33.957674, 18.460687); //Coordinates of Jameson Hall

    GoogleMap map;
    MarkerClass markers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        setCamera(UCTLL,17);    
        //setUpHandler();
        //aHandler.sendEmptyMessageDelayed(0, 5);
        markers.setComSci();
    }

    @SuppressLint("NewApi")
    private void setCamera(LatLng startingpoint, int zoom){
        map = ((MapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(UCTLL, 17));//Set Zoom level
        map.getUiSettings().setRotateGesturesEnabled(false);// Often rotates when trying to zoom, so rotations are diabled
        CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(startingpoint)      
        .zoom(zoom)               
        .bearing(90)                
        .tilt(20)                   // Sets the tilt of the camera 
        .build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }`

And here is the Marker class

public class MarkerClass extends MapActivity{

    String itemtype;
    LatLng coordinates;
    String name;

    public void setComSci(){

        name = "Computer Science Building";
        itemtype = "Building";
        coordinates= new LatLng(-33.956812, 18.461021);
        Marker comscimark = map.addMarker(new MarkerOptions()
        .position(coordinates)
        .draggable(true)
        .title(name));

    } 
  }

If I move the "setComSci()" method into the main class, it works perfectly, but if I run the code as it is now, I get:

`09-11 19:32:22.737: E/AndroidRuntime(11729): FATAL EXCEPTION: main
09-11 19:32:22.737: E/AndroidRuntime(11729): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.map.uctmapfinal/com.map.uctmapfinal.MapActivity}: java.lang.NullPointerException
09-11 19:32:22.737: E/AndroidRuntime(11729):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
09-11 19:32:22.737: E/AndroidRuntime(11729):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
`

Am I being dumb? What am I missing?

EDIT:

Here is activity_map.xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"> 


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

    <ImageButton
        android:id="@+id/searchbutton"
        android:layout_width="50dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@null"
        android:onClick="searchmethod"
        android:scaleType="fitXY"
        android:src="@drawable/search" />

    <ImageButton
        android:id="@+id/filterrbutton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:contentDescription="@null"
        android:drawable="@drawable/search"
        android:onClick="choosefilter"
        android:scaleType="fitXY"
        android:src="@drawable/filter"
        android:state_pressed="true" />

</RelativeLayout>
1
  • can you post the xml for R.layout.activity_map Commented Sep 11, 2014 at 18:04

1 Answer 1

1

the problem is that in setComSci() you are using map.addMarker but map is in MapActivity class.

markers = new  MarkerClass(); //<--- ADD THIS
markers.setComSci(map); //<-- Call using this statement

public class MarkerClass extends MapActivity{
String itemtype;
LatLng coordinates;
String name;
GoogleMap map;

public void setComSci(GoogleMap map){
    this.map = map;
    name = "Computer Science Building";
    itemtype = "Building";
    coordinates= new LatLng(-33.956812, 18.461021);
    Marker comscimark = map.addMarker(new MarkerOptions()
    .position(coordinates)
    .draggable(true)
    .title(name));
  } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

okay, keep that line, its required to initialize the MakerClass, have you added MapActivity in your app manifest?
in setComSci() you are using map.addMarker but map is in MapActivity class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.