I am integrating maps in my application.
The map is also shown in the application.
But I am not able to find its view.
This is my fragment class.
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.search_fragment,
container, false);
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
// googleMap.setMyLocationEnabled(true);
// googleMap.getUiSettings().setZoomControlsEnabled(false);
return rootView;
}
}
I am getting the null pointer exception..
Log cat is as follows:
FATAL EXCEPTION: main
: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.herobike.main/com.herobike.service.SearchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4803)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.herobike.service.SearchActivity$PlaceholderFragment.onCreateView(SearchActivity.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
at android.app.Activity.performStart(Activity.java:5018)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
Line No: 52 is
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
This is xml of fragment
<RelativeLayout 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:background="#drawable/sc_10"
tools:context="com.herobike.main.SearchActivity$PlaceholderFragment" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="2dp" />
What needs to be updated here?
In your layout file instead of MapFragment you need to write SupportMapFragment as you have already accessed it using SupportMapFragment .So change it as below:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="2dp" />
Also initialize your map as below:
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
Check if you have referenced the id of fragment right.
public final GoogleMap getMap ()
Gets the underlying GoogleMap that is tied to the view wrapped by this fragment.
Returns
the GoogleMap. Null if the view of the fragment is not yet ready.
This can happen if the fragment lifecyle have not gone through
onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also
happen if Google Play services is not available. If Google Play
services becomes available afterwards and the fragment have gone
through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this
method again will initialize and return the GoogleMap.
It is better to chekc the availability of google play services before initializing map object.
Edit:
yes inside fragment
What you have done
android:name="com.google.android.gms.maps.MapFragment"
Its a MapFragment.
But you have
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
which is wrong
What you need to do
So you want map inside a fragment. So you need to extend MapFragment instead of Fragment or you should use MapView
Check the below
Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment
pLease try this ,let me know if any concern
if (googleMap == null)
{
googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_history)).getMap();
}
try this
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
and make the following changes in your layout
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
use supportmapfragment :-
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="250dp"
class="com.google.android.gms.maps.SupportMapFragment" />
Related
This is my layout file:
My xml file which contains the map layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</FrameLayout>
My FragmentClass
public class RouteFragment extends Fragment {
GoogleMap map;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_layout,container,false);
// Getting reference to SupportMapFragment of the activity_main
SupportMapFragment fm = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
// Getting Map for the SupportMapFragment
map = fm.getMap();
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(MainActivity.lng1);
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
map.addMarker(options);
options.position(MainActivity.lng2);
map.addMarker(options);
map.moveCamera(CameraUpdateFactory.newLatLng(MainActivity.lng1));
map.animateCamera(CameraUpdateFactory.zoomTo(8));
if(getArguments().getInt("Driving")==1)
map.addPolyline(MainActivity.lineOptions);
else {
Polyline line = map.addPolyline(new PolylineOptions()
.add(MainActivity.lng1, MainActivity.lng2)
.width(5)
.color(Color.RED));
}
return v;
}
}
Here is the exception:
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
Manifest file:
I have requested the desired permissions and defined the uses-library for incroporating maps in the application.
<uses-library android:name="com.google.android.maps"/>
<permission
android:name="com.city.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.city.permission.MAPS_RECEIVE" />
I am having a FragmentActivity, where I have successfully implemented a Google Map through my XML with this code:
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
However, if I try to initialize the map to find a persons location with this code:
GoogleMap googleMap;
googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.googleMap))).getMap();
It is throwing a null pointer exception, as I have understood from other questions regarding this error, it shouldn't be a problem initializing the map when it is created in the XML file. And yes I have used
setContentView(R.layout.map);
Before I initialize the map.
You use the Support Fragment So in your XML File Add
this Line In Fragment
class="com.google.android.gms.maps.SupportMapFragment"
like belove.
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
and Put This
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap)).getMap();
The error is due to incorrect variable initialization try to change your code as shown below,thanks
private GoogleMap gMap;
MapFragment googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_search);
googleMap= (MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap);
gMap= googleMap.getMap();
In you xml you are using MapFragment.Change it -
<fragment
android:id="#+id/mapfragment"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
class="com.google.android.gms.maps.SupportMapFragment" />
java -
googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();
I'm trying to use google map in fragment, but I meet a problem I don't understand.
I don't arrive to load the map in this line :
gMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
This line seems to return always null...
I tried to use MapFragment inseatd of SupportMapFragment, but it's not possible.
How to resolve it ?
To explain why i want to show it in a fragment, it's because i'm using a navigationdrawer, and one of my item needs to show the map.
thx,
EDIT : My xml :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
I think you should call with getsupportFragmentManager not with getFragmentManager otherwise it will return null always
If you are using SupportMapFragment, you need to change your layout!
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
This is a basic example using SupportMapFragment:
public class MainActivity extends ActionBarActivity implements OnMapReadyCallback{
private SupportMapFragment map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map.getMapAsync(this);//remember getMap() is deprecated!
}
#Override
public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(47.17, 27.5699), 16));
map.addMarker(new MarkerOptions() .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)).anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.position(new LatLng(47.17, 27.5699))); //Iasi, Romania
map.setMyLocationEnabled(true);
}
}
and change the reference in your layout:
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
I dont think it is possible as google map contains a < fragment> tag. See here why:
Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.
Some people in SO however managed to show map in a fragment, but the solution was hacky.
I am having trouble with my android app.
I am trying to switch between two tabs which one of them should show a Map.
but the thread is exiting with uncaught exception.
When i debug i get this:
Binary XML file line #7: Error inflating class
com.google.android.gms.maps.MapView
I looked at every possible google search result but I coudlnt find the answer.
this is my mapclass
public class MapFragment extends Fragment {
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapview_tab, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
MapsInitializer.initialize(this.getActivity());
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(47.223065, 8.816511), 10);
map.animateCamera(cameraUpdate);
return v;
}
}
this is my xml file mapview_tab
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<com.google.android.gms.maps.MapView android:id="#+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
and this dependency i added to the pom.xml file
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>google-play-services</artifactId>
<version>16.0.0</version>
<scope>provided</scope>
</dependency>
and here are some important code writings out of my manifest:
<permission android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" >
</uses-feature>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="dsvfdfsAdsfasdfcvdasVGJfsDSSDzcfdASWD"/>
<uses-library android:name="com.google.android.maps"/>
the googleplaylibraray jar file is a "non-runtime maven dependenciy"
I HOPE SOMEONE CAN HELP ME OUT!!
kidn regards,
trash
------EDIT-----------
logcat:
VFY: unable to resolve static field 1308 (MapAttrs) in
Lcom/google/android/gms/R$styleable;
VFY: replacing opocode 0x62 at 0x000e
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x41b81700)
EDIT 2 -----------------------------
NEW XML FILE
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
MAP CLASS
public class MapFragment extends Fragment {
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapview_tab, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.MapView);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
MapsInitializer.initialize(this.getActivity());
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(47.223065, 8.816511), 10);
map.animateCamera(cameraUpdate);
return v;
}
}
LOGCAT:
05-13 14:47:50.189: D/AbsListView(16376): unregisterIRListener() is called
05-13 14:47:51.179: D/AbsListView(16376): onDetachedFromWindow
05-13 14:48:20.009: D/AndroidRuntime(16376): Shutting down VM
05-13 14:48:20.009: W/dalvikvm(16376): threadid=1: thread exiting with uncaught exception (group=0x41b81700)
after clicking on the MAP TAB this is the last I get, then my phones freezes.
Post device version also it may be due to extends fragment class, if you are using devices for testing with lower version you have extend
public class MapFragment extends
android.support.v4.app.Fragment
check this link https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2
---guidlines by google
way to go:--> use mapfragmet instead of mapview....
update
For Map class
public class MapFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapview_tab, container, false);
return v;
}
}
I have dynamically added my google map fragment in framelayout. Now I want to put markers and do other stuff with google map. but i am not able to access that through get fragment manager. The code shows no error but when it executes it returns null pointer exception.
I thinks its logical error. Something to do with type casting. plz help
Here is my "nearby_places.xml" layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.localiteproximus.CustomAutoCompleteTextView
android:id="#+id/atv_places"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="#string/str_atv_places"
android:singleLine="true" />
<FrameLayout
android:id="#+id/mapView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="#id/atv_places">
</FrameLayout>
</RelativeLayout>
and this is my class
public class NearByPlaceFragment extends Fragment {
public NearByPlaceFragment(){}
View rootView;
GoogleMap googleMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MapFragment fragment = new MapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapView, fragment).commit();
rootView = inflater.inflate(R.layout.nearby_places, container, false);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
return rootView
}
and LogCat is showing this
58.608: E/AndroidRuntime(12654): FATAL EXCEPTION: main
03-22 03:00:58.608: E/AndroidRuntime(12654): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.localiteproximus/com.localiteproximus.NearbyPlaces}: android.view.InflateException: Binary XML file line #21: Class is not a View com.google.android.gms.maps.MapFragment
03-22 03:00:58.608: E/AndroidRuntime(12654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
03-22 03:00:58.608: E/AndroidRuntime(12654): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.view.View
03-22 03:00:58.608: E/AndroidRuntime(12654): at java.lang.Class.asSubclass(Class.java:1182)
03-22 03:00:58.608: E/AndroidRuntime(12654): at android.view.LayoutInflater.createView(LayoutInflater.java:565)
03-22 03:00:58.608: E/AndroidRuntime(12654): ... 23 more
I got answer on my own.
So it was really silly mistake.. after 6 hours i found that the error was on this line
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();
which was getting its parent view instead of child so replaced it with this line
googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.mapView)).getMap();
and everything is fine :).