Reputation: 2847
I'm just trying to enable the My Location button on google maps, but the app keeps crashing when I add this:
googleMap.setMyLocationEnabled(true)
To the method which displays Google maps
Here is my code:
package com.example.safezone;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
public class MainActivity extends FragmentActivity {
GoogleMap mMap;
LatLng myPosition;
LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void mapSearch(View v) {
setContentView(R.layout.activity_search);
}
public void myLocation(View v) {
setContentView(R.layout.activity_my_location);
mMap.setMyLocationEnabled(true);
}
}
Errors:
11-25 14:42:27.865: E/AndroidRuntime(3521): FATAL EXCEPTION: main
11-25 14:42:27.865: E/AndroidRuntime(3521): java.lang.IllegalStateException: Could not execute method of the activity
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View$1.onClick(View.java:3691)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View.performClick(View.java:4211)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View$PerformClick.run(View.java:17267)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.os.Handler.handleCallback(Handler.java:615)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.os.Looper.loop(Looper.java:137)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:42:27.865: E/AndroidRuntime(3521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-25 14:42:27.865: E/AndroidRuntime(3521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-25 14:42:27.865: E/AndroidRuntime(3521): at dalvik.system.NativeStart.main(Native Method)
11-25 14:42:27.865: E/AndroidRuntime(3521): Caused by: java.lang.reflect.InvocationTargetException
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View$1.onClick(View.java:3686)
11-25 14:42:27.865: E/AndroidRuntime(3521): ... 11 more
11-25 14:42:27.865: E/AndroidRuntime(3521): Caused by: java.lang.NullPointerException
11-25 14:42:27.865: E/AndroidRuntime(3521): at com.example.safezone.MainActivity.myLocation(MainActivity.java:31)
11-25 14:42:27.865: E/AndroidRuntime(3521): ... 14 more
The method executes fine if I don't add setMyLocationEnabled(), and Google Maps comes up.
Upvotes: 0
Views: 4186
Reputation: 73916
it is crashing because you did not get the map so your mMap
object is null when you try to access it
mMap = getSupportFragmentManager().findFagmentById(r.id.yourfragment).getMap();
Upvotes: 0
Reputation: 18068
your variable mMap
is null. You have not assigned any object instance to it. Based on your layout xml, you need to assign value to it or initialise it.
Upvotes: 1