I'm trying to write a class which is to handle the maps methods (showmap, addmarker, etc) and I call these methods in an Activity or Fragment, but I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
I know what it means, but I canit solve it.
So I hope somebody can help me.
Here is the map calss:
public class MapHandle extends AppCompatActivity implements LocationListener {
private SupportMapFragment mapFragment;
private GoogleMap map;
public void initMap() {
mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
if (mapFragment != null) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
createMap(map);
}
});
} else {
Toast.makeText(this, "Error - Map Fragment was null!!", Toast.LENGTH_SHORT).show();
}
}
public void createMap(GoogleMap googleMap) {
map = googleMap;
if (map != null) {
// Map is ready
Toast.makeText(this, "Map Fragment was loaded properly!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Error - Map was null!!", Toast.LENGTH_SHORT).show();
}
}
}
And here I call it in an Activity:
public class FragOne extends Fragment {
MapHandle mh;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mh = new MapHandle();
mh.initMap();
return inflater.inflate(R.layout.frag1,container,false);
}
}