0

My Application keeps crashing once it opens in nexus 6. The Problem is with getMapAsync().

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { GoogleMap mMap; //member variable private static final int ERROR_DIALOG_REQUEST = 9001; //constant to request certain king of dialog box definition from google play services lib private static final double AJAX_LAT = 43.851063, AJAX_LNG = -79.019737; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (servicesOK()) { setContentView(R.layout.activity_main); if (initMap()) { Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show(); gotoLocation(AJAX_LAT, AJAX_LNG, 13); //as of now user must grand permissions to app from phone settings try { mMap.setMyLocationEnabled(true); } catch (SecurityException e) { Toast.makeText(this, "My Location not enabled!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "Map not connected!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "Services not OK!", Toast.LENGTH_SHORT).show(); } } //get a reference to the map object public boolean initMap() { if (mMap == null) { MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } return (mMap != null); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; } //check to see if the google services are ok public boolean servicesOK() { GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); int isAvailable = googleAPI.isGooglePlayServicesAvailable(this); // handle 3 different possibility if (isAvailable == ConnectionResult.SUCCESS) { //everything ok, user can make mapping request return true; } else if (googleAPI.isUserResolvableError(isAvailable)) { //error user can do something about Dialog dialog = googleAPI.getErrorDialog(this, isAvailable, ERROR_DIALOG_REQUEST); dialog.show(); //google play services lib delivers correct dialog box telling user what to do } else { //something wrong, user cant do anything about Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show(); } return false; } } 

Below is the error i get

Process: com.example.suhail.onmyway, PID: 27425 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.suhail.onmyway/com.example.suhail.onmyway.MainActivity}: java.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 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.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 at com.example.suhail.onmyway.MainActivity.initMap(MainActivity.java:89) at com.example.suhail.onmyway.MainActivity.onCreate(MainActivity.java:47) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

i have a button in activity_main.xml that once clicked opens another layout where a fragment is included

3
  • 1
    Usually with AppCompatActivity, you use SupportMapFragment and getSupportFragmentManager(). Beyond that, you might wish to post your activity_main layout. Commented Jun 5, 2016 at 22:34
  • i used SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mMap = mapFragment.getMap(); before,but getMap() is now deprecated Commented Jun 5, 2016 at 22:46
  • Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Jun 5, 2016 at 23:02

3 Answers 3

0

Probably your mapFragment is null, also incorrect getMapAsync usage, you must use all that belongs to map only after you receive the map, so you must act only after this call back is called:

@Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; } 

add all next initialisations in runnable and call it after

mMap = googleMap; 
Sign up to request clarification or add additional context in comments.

Comments

0

You have to add this to your activity_main.xml

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

1 Comment

i have a button in activity_main.xml that once clicked opens another layout where a fragment is included <fragment xmlns:android="schemas.android.com/apk/res/android" xmlns:map="schemas.android.com/apk/res-auto" android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/searchButton" />
0

Put setContentView out of if block It crashes from line 89 What is code of this line ?

------------ Update ------------

Use It :

 GoogleMap map = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); final LatLng GOOGLE_MAP_POSITION = new LatLng(lat, lng); Log.d("GOOGLE_MAP", "lat : " + lat + " , lng : " + lng); if (map != null) { map.clear(); map.setMapType(GoogleMap.MAP_TYPE_NORMAL); Marker marker = map.addMarker(new MarkerOptions().position(GOOGLE_MAP_POSITION) .title("Title") .snippet("Content Goes Here") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); } MapsInitializer.initialize(this); // Move the camera instantly to target with a zoom of 14. map.moveCamera(CameraUpdateFactory.newLatLngZoom(GOOGLE_MAP_POSITION, 50)); // Zoom in, animating the camera, Change 14 to Zoom in And Zoom Out map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null); int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); if (resultCode == ConnectionResult.SUCCESS){ Log.e("DETAIL_ACTIVITY", "Google Play Services Util Available SUCCESS : "+resultCode); }else{ Log.e("DETAIL_ACTIVITY", "Google Play Services Util Available Problem : "+resultCode); GooglePlayServicesUtil.getErrorDialog(resultCode, this, 1); } 

Also WorkaroundMapFragment is a Custom Class that Let you to use map inside ScrollView

To use it check it out.

1 Comment

line 89 MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.