Android google maps fragment issue - android

I am implementing Google maps in my android app. It works well but sometimes the app crashes and gives me this error: (Caused by: android.view.InflateException: Binary XML file line #2 in Error inflating class fragment).
I tried all solutions suggested but most of them are old and I still has the same issue.
Can you please help me with this issue while it still cause app crash.
this is the code used in Google Maps:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityTruckLocationBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
lat=lang="0";
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng myplace = new LatLng(29.2722744, 47.8585885);
//mMap.addMarker(new MarkerOptions().position(myplace).title("Marker in myplace"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myplace,11));
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(#NonNull LatLng latLng) {
lat=String.valueOf(latLng.latitude);
lang=String.valueOf(latLng.longitude);
redirect();
}
});
}
public void redirect(){
Intent intent=new Intent(TruckLocation.this,Truck_Profile.class);
intent.putExtra("intenttype","location");
intent.putExtra("lat",lat);
intent.putExtra("lang",lang);
startActivity(intent);
finish();
}
Here is the XML code:
<?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/map2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TruckLocation" />

You have an issue in TruckLocationActivity.xml , you can share this xml with us to help you .

Related

Google Maps Display

Iam practicing on Google Maps for android development. I have followed the steps as in https://developers.google.com/maps/documentation/android-api/map-with-marker
While when playing the app I got the screen like this below, no marker and the map cannot show any thing
My code as following:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
******** . XML Layout ************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ae.map_04.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
This is most likely an API Key issue. You can find some guidance here: Android Studio - Google map still blank on real Android device on release apk
If that does not work, try a new API Key.

Here Maps - use MapFragment in a Fragment of TabLayout

My application has 3 tabs and in one of them I want to display a map(HERE MAPS, not Google Maps). I was able to get the map to work perfectly in an Activity, but in Fragment Class when I try to cast the Fragment view to the MapFragment an error is thrown.
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
Error:
Inconvetible types: cannot cast android.support.v4.app.support to com.here.android.mpa.mapping.MapFragment
Please correct me if Im wrong, but the reason for this to happen is because we can´t use android.app.Fragment(used to display the map, as instructed in HERE MAPS DOC) in an android.support.v4.app.Fragment(used for the TabsLayout).
I found many questions related to this error using Google Maps. but just two(first,second)about the same error when using HERE MAPS, and none really helped to solve the problem.
In google Maps you can use SupportMapFragment(), but this mehod only works for Google Maps. Is there any solution for this using Here Maps? maybe a different approach to reach the same goal? or am I missing something when implementing this Here Maps in a TabLayout?
Any help will be appreciated!
My code:
MapFragment.java
public class Map extends Fragment {
public Map() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
com.here.android.mpa.mapping.Map map = mapFragment.getMap();
} else {
System.out.println("ERROR: Cannot initialize MapFragment");
}
}
};
return view;
}
fragment_map.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"
tools:context="com.example.modulos.tabsMenu.Config">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/mapFragmentContainer"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<fragment
class="com.here.android.mpa.mapping.MapFragment"
android:id="#+id/mapfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<!-- other things here -->
</LinearLayout>
</FrameLayout>
Since the HERE Maps SDK only works with API Level 15 and above, there's no support for compatibility fragments (that you mainly need when you target lower API levels).
In some cases you still want to deal with support fragment even if you have Android 4 or newer as target, in those cases you have to use the the MapView of the HERE SDK and embedded it in your layout on your own.
See this code example where also MapView is used: https://tcs.ext.here.com/sdk_examples/MapDownloader.zip
So, it will look like this:
Add in your layout the MapView:
<com.here.android.mpa.mapping.MapView
android:id="#+id/ext_mapview"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And then in your code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = (MapView) findViewById(R.id.ext_mapview);
MapEngine.getInstance().init(this, engineInitHandler);
}
private OnEngineInitListener engineInitHandler = new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(Error error) {
if (error == Error.NONE) {
map = new Map();
mapView.setMap(map);
// more map initial settings
} else {
Log.e(TAG, "ERROR: Cannot initialize MapEngine " + error);
}
}
};
#Override
public void onResume() {
super.onResume();
MapEngine.getInstance().onResume();
if (mapView != null) {
mapView.onResume();
}
}
#Override
public void onPause() {
if (mapView != null) {
mapView.onPause();
}
MapEngine.getInstance().onPause();
super.onPause();
}
It's important that you handle the MapEngine and MapView Pause and Resume, otherwise you would get poor performance results.
Surely is a silliness ask this, but do you check if are you using MapFragment from the right package?
You must be using this class com.here.android.mpa.mapping.MapFragment, maybe by error you import this one com.google.android.gms.maps.MapFragment.

Google maps error inflating, Android

I get the following error when I try to leave fragment which contains map (on back button press).
ERROR: mapInflater->Binary XML file line #7: Error inflating class fragment
Bellow is onCreate() method of fragment where the map is placed. And also there is a xml code of layout.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
apiRequestManager = ApiRequestManager.getInstance();
fragmentHandler = FragmentHandlerActivityMain.getInstance(getActivity());
session = new SessionManager(App.getInstance().getApplicationContext());
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.maps_fragment_toolbar_name));
Handler mainHandler = new Handler();
Runnable myRunnable = new Runnable() {
#Override
public void run() {
try {
SupportMapFragment maps = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.mapView);
if (maps != null)
maps.getMapAsync(MapsFragment.this);
} catch (Exception ex) {
LogUtil.getInstance().add(LogUtil.ERROR,
TAG, "onCreateMaps", ex);
}
}
};
mainHandler.post(myRunnable);
}
Log is pointing on this layout on fragment (line 7).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".salesman.fragments.MapsFragment">
<fragment
android:id="#+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</FrameLayout>
You are trying to getChildFragmentManager() instade of getSupportFragmentManager().
Need to change In Java File :
1). You need to implements OnMapReadyCallback in Your Fragment.
2). Get SupportMapFragment from View.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
3). Overrid onMapReady(GoogleMap googlMap) Method.
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// and move the map's camera to the same location.
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
In XML File :-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app1="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".salesman.fragments.MapsFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
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"/>
</FrameLayout>
Please check the updated answer as per below comment:
Now try to set MapView insted of fragment because if you use fragment inside fragment it creates lot of issues.
Need to change in JAVA Code : // Initialize GoogleMap myMap; MapView mMapView;
// OnCreateView Method MapsInitializer.initialize(this.getActivity()); mMapView = (MapView) rootView.findViewById(R.id.map); mMapView.onCreate(savedInstanceState); mMapView.getMapAsync(this);
#Override public void onPause() { super.onPause(); mMapView.onPause(); }
#Override public void onPause() { super.onPause(); mMapView.onPause(); }
#Overridepublic void onResume() { super.onResume(); mMapView.onResume(); }
Need to change in XML File :
<com.google.android.gms.maps.MapView android:id="#+id/map" android:layout_width="match_parent" android:layout_height="match_parent" />

Maps API Fragment not showing Marker in DrawerLayout

I'm new with StackOverflow and Maps API. I'm developing an app where a Drawer Layout is being used. I want to use one of the Drawer fragments to show Maps and work with Markers.
private SupportMapFragment mapFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapFragment = (SupportMapFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps, container, false);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
this is the fragment_maps.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"
tools:context="com.labcontrol.lca.view.MapsFragment">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
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.labcontrol.lca.controller.MainActivity" />
</FrameLayout>
In the official guide they made an example using an Activity, as you can see I tried to do it inside a fragment. is it possible?
The result is this: maps is working properly, but I can't see the Marker I created, and I spent too many hours trying to figure out why.
The only thing make me suspicious is this error when playing with Maps on the phone and changing the camera:
W/Google Maps Android API: GLHudOverlay deprecated; draw(): no-op
Found out it's a common issue these days, a very new bug. Somebody knows how to get it up and running? Is there something wrong with the code?
thanks a lot

Mapview displays blank but fragment is ok

I am trying to use Google Map V2. When I use map fragment .. Everything works.
However if I use mapview instead, then it displays blank activity.
What could be wrong?
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapParent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
You need to initialize the map yourself:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Gets the MapView from the XML layout and creates it
MapView mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
GoogleMap map = mapView.getMap();
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
MapsInitializer.initialize(this);
mapView.onResume();
}
about MainActivity - If you only wanted to display simple map UI you can do following:
private GoogleMap map; // or private static GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[...]
try {
loadingObjectOfMainMap();
} catch (Exception e) {
e.printStackTrace(); // prints error to console/log
}
[...]
}
and then method:
protected void loadingObjectOfMainMap() {
if( map == null) {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true); // will display blue dot at your location
}
}
About XML:
I highly recommed you to use fragments for google maps objects, like this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapOptions="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
mapOptions:mapType="hybrid"/>
I recommend you simple tut, you will find all answers there
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/
(no, it's not my site - it's place where I started working with google maps api)

Categories

Resources