Reputation: 850
I have been trying to solve this issue for ages and I can't find where the problem is. I am having trouble only with some devices that run Android 2.2. I tested on Android 4.1.2 and it works fine.
The error I am getting:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coldice.plotfinder/com.coldice.plotfinder.MapFragment}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
at android.app.ActivityThread.access$2300(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4632)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.coldice.plotfinder.MapFragment.onCreate(MapFragment.java:67)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2632)
... 11 more
So looking back to my class MapFragment line 67 which is
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
I am aware that googlemap API v2 requires API level 12 or higher as noted here, but I am using lower so I called
getSupportFragmentManager();
The MapFragment class code is below:
public class MapFragment extends SherlockFragmentActivity {
private static final String TAG = "MapFragment";
private GoogleMap googleMap; // Google map
private int mapType = GoogleMap.MAP_TYPE_SATELLITE;
private Polyline polyline;; // Drawing the area of the land by using the polygon
private boolean isDrawn=false;
static String fileToBeRead =null; // The file name to be read
ArrayList<Cooridnates> cooridnatesList;
// Image utility saving
ImageSaveUtil imageUtil = new ImageSaveUtil();
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// Different colors for each marker
float[] markerColours = {BitmapDescriptorFactory.HUE_AZURE,BitmapDescriptorFactory.HUE_BLUE,BitmapDescriptorFactory.HUE_CYAN,BitmapDescriptorFactory.HUE_GREEN,
BitmapDescriptorFactory.HUE_MAGENTA,BitmapDescriptorFactory.HUE_ORANGE,BitmapDescriptorFactory.HUE_RED,BitmapDescriptorFactory.HUE_ROSE,
BitmapDescriptorFactory.HUE_VIOLET,BitmapDescriptorFactory.HUE_YELLOW};
// The code I am getting java.lang.NullPointerException
FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment)fragmentManager.findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// If there is a file to be read, then read the coordinates
if(fileToBeRead!=null)
{
readData(fileToBeRead);
fileToBeRead =null;
}
// Getting the coordinates List
cooridnatesList = MainActivity.getList();
if(cooridnatesList!=null)
{
// Adding a marker from each point
for(int i=0;i<cooridnatesList.size();i++)
{
LatLng point = new LatLng(cooridnatesList.get(i).getLat(),cooridnatesList.get(i).getLon());
googleMap.addMarker(new MarkerOptions()
.position(point)
.title("My Land")
.snippet("Point: "+cooridnatesList.get(i).getLat()+","+cooridnatesList.get(i).getLon())
.icon(BitmapDescriptorFactory.defaultMarker(markerColours[i%10])));
}
googleMap.getUiSettings().setCompassEnabled(true); // Setting the compass enabled in the map
googleMap.getUiSettings().setZoomControlsEnabled(true); // Zooming is enabled
googleMap.getUiSettings().setMyLocationButtonEnabled(true); // My location button is enabled
// Zooming the camera from the first point entered
LatLng cameraLatLng = new LatLng(cooridnatesList.get(0).getLat(),cooridnatesList.get(0).getLon());
float cameraZoom = 10;
if(savedInstanceState != null){
mapType = savedInstanceState.getInt("map_type", GoogleMap.MAP_TYPE_SATELLITE);
double savedLat = savedInstanceState.getDouble("lat");
double savedLng = savedInstanceState.getDouble("lng");
cameraLatLng = new LatLng(savedLat, savedLng);
cameraZoom = savedInstanceState.getFloat("zoom",18);
}
// Setting the map type such as satellites or street view
googleMap.setMapType(mapType);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cameraLatLng, cameraZoom));
//Displaying the land area
final TextView landInformation = (TextView)findViewById(R.id.textViewLandInformation);
double area = calculateArea(cooridnatesList);
landInformation.setText(getString(R.string.land_area)+"\n"+Math.round(area)+" SQ.M");
}
}
Upvotes: 1
Views: 1869
Reputation: 5093
Try moving all code that references your GoogleMap to onStart() or onResume(). The map in a map fragment isn't instantiated until after the fragment has gone through onCreateView (link). Also, you need to check your googleMap for null, because if google play services aren't installed, or the map isn't available for some other reason, it will be null.
Upvotes: 2