Map Fragment Class doesnt infalte! Getting InflateException - android

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;
}
}

Related

Android application crashes on trying to inflate map layout on some devices but works fine on latest devices

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" />

getMapAsync() in Fragment

I having trouble implementing Google Map in Fragment.
This is my the part of my fragment class:
public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback {
// Google Map
private GoogleMap googleMap;
private int i;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(
R.id.map)).getMapAsync(this);
I am getting an error in getMapAsync(this). I tells Incompatible type.
it says:
required: com.google.android.gms.map.GoogleMap
found: void
BTW here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
In your XML layout you should do like this
<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">
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
inside your fragment implement this
private MapView mapView;
private GoogleMap googleMap;
override onCreateView if there is no onCreateView just like the code below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.your_layout, container, false);
}
override onViewCreated() inside that
onViewCreated() put this
mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment
when you already implement OnMapReadyCallback in your fragment put this/code like this
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
hope this helps you.
After a few overheats in my head, thanks to this post and https://stackoverflow.com/a/34732804/3925554, I would say that currently there are two ways of adding a MapFragment to your app:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
By this way, you have to use a standard fragment and implement this inside. Actually you are inserting a fragment into another:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
}
The other way would be extending the real SupportMapFragment and implement it in this manner without the need of an xml:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getMapAsync(this);
}
And they work without having to set a layout in onCreateView neither call mapView.onCreate(savedInstanceState); and mapView.onResume(); manually inside onViewCreated(), what is not recommended anyway.
Hope it helps!
SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flMap, mMapFragment);
fragmentTransaction.commit();
mMapFragment.getMapAsync(this);
Your code is returning this error because getMapAsync() does not return anything. If you look at the documentation for this function:
public void getMapAsync (OnMapReadyCallback callback)
Sets a callback object which will be triggered when the GoogleMap
instance is ready to be used.
Note that:
This method must be called from the main thread. The callback will be
executed in the main thread. In the case where Google Play services is
not installed on the user's device, the callback will not be triggered
until the user installs it. In the rare case where the GoogleMap is
destroyed immediately after creation, the callback is not triggered.
The GoogleMap object provided by the callback is non-null.
OnMapReadyCallback is an interface that needs implemented and passed to through this function. Nothing is currently assigned to your googleMap variable you should instead set its value in this block of code which implements OnMapReadyCallback
#Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
}
This code should be in your Fragment somewhere. Since your Fragment already implements this interface you do pass it in as this.

How to use google map in android fragment?

I am developing an app that use fragments. One of my fragments i want to use google map but i am getting errors.
public class Map extends Fragment {
public Map() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
GoogleMap map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//Error:(38, 84) error: inconvertible types
//required: SupportMapFragment
//found: Fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
}
When i trying to cast map fragment i am getting the error in comment. What should i do to solve this? Thanks for everyone.
Edit: the layout ;
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
You can go through this approach.This is my Fragment class which has MapView
public class DashBoardFragment extends Fragment{
//-------------- class variables
private MapView mMapView;
private GoogleMap mGoogleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_dash_board, container, false);
mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mGoogleMap = mMapView.getMap();
}
}
Below is my xml for the fragment
<LinearLayout 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:orientation="vertical"
tools:context="com.dev.abc">
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_dashBoard"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Manifest should have the following along with permissions
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your api key" />
You are casting to a mapFragment AFTER the return statement. Since the program believes the method is complete at that point it never reaches it. Try this:
Inflater v = inflater.inflate(R.layout.fragment_map, container, false);
GoogleMap map = ((SupportMapFragment)v.getFragmentManager().findFragmentById(R.id.map)).getMap();
return v;
Also I don't see you using an intent to start the google map activity.
Latest update of Google for Maps, only MapView is supported for fragments.
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Null pointer exception using maps API2 android?

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" />

MapView in fragment error

I get an android.inflate.Exception when inflate this .xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="***"
/>
This class inflate the .xml (fragment_b.xml)
public class BFragment extends Fragment {
private MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_b, container, false);
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();//needed to get the map to display immediately
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
return v;
}
}
I have try a lot of thing with the .xml without results.
Aren't you mixing map api V1 and map api V2 implementations?
This is map v1:
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="***"
/>
This is how map api V2 would look like:
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Since in your XML you have created an instance of map_v1, you are not allowed to invoke onCreate() and onResume() - you have to do that for map_v2 though.
Also you should remember that in map_v2 you must call onCreate(), onResume() only when appropriate event happens in your activity/fragment lifecycle... So you must call those only from your activity/fragment onCreate and onResume

Categories

Resources