Phil
Phil

Reputation: 83

Android Google Maps Fatal Exception

So i am trying to create a android app that will read in lots of flight data and represent the data on Google maps with markers.

Currently im stuck not being able to do much, i can generate a app that displays Google maps, and have been able to set a start location using XML.

I can't seem to add markers though, it just crashes.

package com.fly.plane;

import com.fly.plane.R;
import com.google.android.gms.maps.GoogleMap;

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


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

public class MyMapActivity extends FragmentActivity {
GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_map);

// without this next line, it will work:
        mMap.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Hello world"));
}

}

and the XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

</RelativeLayout>

Here's the error log:

02-10 22:31:28.433: E/AndroidRuntime(13131): FATAL EXCEPTION: main
02-10 22:31:28.433: E/AndroidRuntime(13131): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fly.plane/com.fly.plane.MyMapActivity}: java.lang.NullPointerException
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.os.Looper.loop(Looper.java:137)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.ActivityThread.main(ActivityThread.java:5419)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at java.lang.reflect.Method.invokeNative(Native Method)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at java.lang.reflect.Method.invoke(Method.java:525)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at dalvik.system.NativeStart.main(Native Method)
02-10 22:31:28.433: E/AndroidRuntime(13131): Caused by: java.lang.NullPointerException
02-10 22:31:28.433: E/AndroidRuntime(13131):    at com.fly.plane.MyMapActivity.onCreate(MyMapActivity.java:21)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.Activity.performCreate(Activity.java:5372)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-10 22:31:28.433: E/AndroidRuntime(13131):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
02-10 22:31:28.433: E/AndroidRuntime(13131):    ... 11 more

Im not very strong at java/android development so any help on what is causing this would be appreciated.

Regards

Phil

Upvotes: 1

Views: 969

Answers (5)

Pihu
Pihu

Reputation: 1039

Add the initialization of Map object in onCreate Method of your activity:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

Upvotes: 0

Namrata
Namrata

Reputation: 1684

You forgot to initialize map :

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

Write it in your onCreate() menthod

Upvotes: 1

M D
M D

Reputation: 47817

Crashing your app because you forget to initialized a Map Object. You should add like

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

into your Activity onCreate(). That's it.

Upvotes: 1

Jorgesys
Jorgesys

Reputation: 126563

a line is missing:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

Your onCreate() method must be like:

private GoogleMap mMap; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_map);

//Add this line.
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        mMap.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Hello world"));
}

Upvotes: 1

chughes
chughes

Reputation: 81

I use the below to add markers.

mMap = getMap();
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title(tit).draggable(true).snippet(String.valueOf(id))

I think you just need to initialise mMap before adding the marker

Upvotes: 1

Related Questions