Issue
I'm trying create a How to get using MapView. I created a Fragment and a XML to use that. The problem is when I try open the MapView throws a NullPointerException but I can't undertand why.
How could I solve it ?
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginRight="10dp" />
</LinearLayout>
The Fragment
public class FormComoChegarEmpresa extends Fragment{
MapView mapView;
private GoogleMap googleMap;
private final String TAG = getClass().getSimpleName() + "->";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((CustomDrawerLayout)getActivity()).getSupportActionBar().hide();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.como_chegar_empresa, container, false);
mapView = (MapView)view.findViewById(R.id.mapView);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setMapView();
}
private void setMapView() {
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
}
}
Exception
java.lang.NullPointerException
at br.com.package.myapp.frags.FormComoChegarEmpresa.setMapView(FormComoChegarEmpresa.java:80)
at br.com.package.myapp.frags.FormComoChegarEmpresa.onActivityCreated(FormComoChegarEmpresa.java:56)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Solution
I've faced the same issue with my first attempt at GoogleMap and MapView.
The issue is that when you init the map, it takes a short while to load (which it does in the background). When you obtain the instance from the MapView, the value of the GoogleMap is still null (not initialized).
You need to use OnMapLoadedCallback here. Add an instance of this callback to the googleMap
right after googleMap = mapView.getMap();
. The callback body has a function called onMapLoaded()
, wherein you place the following code:
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
and anything as the likes of 'googleMap.<member>'
. I'm adding the edited code below for your ease.
private void setMapView() {
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mapView.getMap();
googleMap.setOnMapLoadedCallback(new OnMapLoadedCallback()
{
@Override
public void onMapLoaded()
{
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
}
});
}
Answered By - SlashG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.