Map Return NULL - android

When Application run always return NULL Exception
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
if (map == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googlemap) {
// TODO Auto-generated method stub
map=googlemap;
// startMap();
}
});
}
Xml Code is here where i used Map *
<fragment
android:id="#+id/map"
class="com.shahi.driver.locationfiles.TouchableMapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

Your class will be:
public class YourActivity extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
try {
// Loading map
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onResume() {
super.onResume();
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
final LatLng current_position = new LatLng(your_latitude, your_longitude);
MarkerOptions marker = new MarkerOptions().position(current_position).title("Your Address").snippet("Anything");
googleMap.addMarker(marker);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current_position, 12));
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.setMyLocationEnabled(true);
}
}
and your_layout.xml will be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

This should work:
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location ).title("Marker in location "));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Then your xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.polaris.project_x.MapsActivity" />

I can see you have used custom Map Fragment on your XML code. By searching through its name I chanced upon its code on GitHub.
The issue on your code is that you are trying to get MapFragment from your layout but your Custom Map code is extending SupportMapFragment.
So to make your code work change your MapFragment to SupportMapFragment.
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
Also if your BaseActivity is extending AppCompatActivity then use getSupportFragmentManager() instead of getFragmentManager().

Related

Google Map with Bottom Navigation in android studio

I'm developing Android Google Map project in Android Studio. I have a MapActivtity to show location(The Blew Code:)
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LatLng marker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Add a marker in Sydney and move the camera
LatLng iran = new LatLng(29, 52);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(iran, 5.0f));
addMarker(29.1,52.2,"ssss");
}
public void addMarker(double Lat,Double Lng, String Title){
marker=new LatLng(Lat,Lng);
mMap.addMarker(new MarkerOptions().position(marker).title(Title));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(marker, 10.0f));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
}
And my layout is like this:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
when the App run, Map Show Correctly, But I want to show Map like below image with bottom navigation. when the map button select, The map show.

Show Google Map in Material Dialog Android

I want to show google map in to a custom dialogue.. But i can not access SupportFragmentManager from dialogue class...
Here is my code....
public class AddressViewDialog extends MaterialDialog.Builder implements OnMapReadyCallback {
private MaterialDialog reviewDialog;
private double latitude = 23.0;
private double longitude = 90.0;
private SupportMapFragment mMapFragment;
private GoogleMap mMap;
public AddressViewDialog(Context context) {
super(context);
this.context = context;
initializeView();
}
public void initializeView() {
reviewDialog = new MaterialDialog.Builder(this.getContext())
.title(R.string.request_to_introduce)
.customView(R.layout.dialog_pending_introducer_review, true)
.show();
View v = reviewDialog.getCustomView();
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
mMapFragment.getView().setVisibility(View.GONE);
if (latitude != 0.0 && longitude != 0.0) {
mMapFragment.getView().setVisibility(View.VISIBLE);
mMapFragment.getMapAsync(this);
} else
mMapFragment.getView().setVisibility(View.GONE);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng currentLoc = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(currentLoc).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLoc, 14.0f));
}
}
Here is my xml..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_margin="#dimen/activity_horizontal_margin">
<fragment
android:id="#+id/fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="250dp" />
</LinearLayout>
It shows error in this line:
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
I also Tried :
mMapFragment = (SupportMapFragment) this.getSupportFragmentManager()
.findFragmentById(R.id.fragment);
You should call getSupportFragmentManager() not on MaterialDialog.Builder object (your AddressViewDialog extends MaterialDialog.Builder) but on AppCompatActivity object. So use
mMapFragment = (SupportMapFragment) ((AppCompatActivity) this.context).getSupportFragmentManager()
.findFragmentById(R.id.fragment);
instead of
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
and create your AddressViewDialog from MainActivity this way:
new AddressViewDialog(MainActivity.this).show();
(MainActivity should extends AppCompatActivity).

Marker is not working(not exact location is displaying), only world Map is visible in Frgament

CustomMapFragment
public class CustomMapFragment extends SupportMapFragment {
public static final LatLng NurissLife = new LatLng(28.602012, 77.098750);
private SupportMapFragment supportMapFragment;
#Override
public void onActivityCreated(Bundle bundle) {
// FragmentManager fm=getChildFragmentManager();
supportMapFragment=SupportMapFragment.newInstance();
//supportMapFragment=(SupportMapFragment)getChildFragmentManager
//().findFragmentById(R.id.map_container);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
super.onActivityCreated(bundle);
}
}
Fragment xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map_content"
tools:context="com.demo.stuartabhi.nurisslife.Fragment.ContactFragment">
<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:layout_width="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_height="match_parent"
android:id="#+id/map_container">
</FrameLayout>
</FrameLayout>'
Marker is not working(exact location is not displaying), only world Map
is visible in Fragement
OnMapReady is not working either showing
no error in log, location code being described in OnMapReady method.
Please help regarding this, how to access the described location LatLng NurissLife inside SupportMapFragment.
Thanks in advance.
If you extend SupportMapFragment, there is no need to inflate any xml layout. Just call this.getMapAsync() in order to get a reference to the GoogleMap:
public class CustomMapFragment extends SupportMapFragment implements
OnMapReadyCallback {
private GoogleMap mMap;
public CustomMapFragment() {
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d("MyMap", "onMapReady");
mMap = googleMap;
mMap.addMarker(new MarkerOptions().position(NurissLife).title("Nuriss LifeCare"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NurissLife, 15));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
}
Try this:
Create a method getCustomMapFragment() in your Activity class like this:
private SupportMapFragment getCustomMapFragment(){
SupportMapFragment supportMapFragment;
supportMapFragment= SupportMapFragment.newInstance();
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
return supportMapFragment;
}
And when you perform your FragmentTransaction, try this:
...
transaction=getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_container,getCustomMapFragment()).commit();
...
Also, with this implementation, the class="com.google.android.gms.maps.SupportMapFragment" line in your XML might be unnecessary.

android google maponClick not responsive

I have a map in my layout which when clicked should addmarker to it and display it on map.But the onMapClick method is not triggered when clicked.Below is the code":
Details.java:
public class Details extends AppCompatActivity implements OnMapReadyCallback,GoogleMap.OnMapClickListener {
SupportMapFragment mapFragment;
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map1);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
map=googleMap; LatLng bolg = new LatLng(9.984616,76.267082);
map.addMarker(newMarkerOptions().position(bolg).title("Demo")).showInfoWindow(); map.moveCamera(CameraUpdateFactory.newLatLng(bolg));
map.animateCamera(CameraUpdateFactory.zoomTo(7), 3000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
#Override
public void onMapClick(LatLng latLng) {
Log.e("LongClick","clicked");
map.addMarker(new MarkerOptions().position(latLng).title("X")).showInfoWindow();
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(7), 3000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
details.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/map1"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</LinearLayout>
To detect user click event on GoogleMap, modify the AppCompatActivity
implements OnMapClickListener, override onMapClick(LatLng point), and
call map.setOnMapClickListener(this).
map.setOnMapClickListener(this);
In your onMapReady set an on click listener map.setOnclicklistener
After getting google map from mapfragment you need to do the following for receiving click events.
GoogleMap mGoogleMap = mapFragment.getMap();
mGoogleMap.setOnMapClickListener(this);
you need to implement this listener also GoogleMap.OnMapClickListener

Can't initialize GoogleMap, returns null

I'm trying to initialize a GoogleMap instance, but SupportMapFragment returns null.
Here is my MainActivity Class:
public class MainActivity extends FragmentActivity
{
GoogleMap gMap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initMap(); //here is the NullPointerExeption
setContentView(R.layout.activity_map);
}
public boolean initMap()
{
if (gMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragMap);
gMap = mapFrag.getMap();
}
return (gMap != null);
}
}
And this is my activity_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
what is the problem? if I comment out the initMap() function, it works fine and displays the map.
I think you should change the order like
setContentView(R.layout.activity_map);
initMap();
First you should setContentView(R.layout.activity_map); and then reference Map from SupportMapFragment.

Categories

Resources