I am trying to embed Google map in Activity. I integrated it a couple of times before but It's not happening right now.
My layout file:
<fragment
android:id="#+id/location_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|center_vertical" />
Map Class
#EActivity(R.layout.location_activity)
public class FIndLocationActivity extends AppCompatActivity implements OnMapReadyCallback {
#AfterExtras
void afterViewCreated() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.location_map);
mapFragment.getMapAsync(this);
.....
.....
}
}
mapFragment remains always Null. What am I doing wrong here? I also followed this tutorial but still it's throwing null. I am using actual device to test the app.
Check if you added all credentials for GMaps in your app, including additional egl 'uses' lines in manifest. This may be the issue why system can't create fragment.
Related
I just created an Google Maps activity by selecting the default Google Maps Activity. However, I found that there is no actionbar on the top.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
My question is how to add an actionbar to it. Do I have to change the extends from FragmentActivity to AppCompatActivity? I am very new to Android, please explain using the most basic languages.
Just extend AppCompatActivity as most of the answers have suggested.
public class MapsActivity extends AppCompatActivity implements
OnMapReadyCallback {
}
Tried it and it worked fine.
If you want to have a detailed answer, I would check the docs, these docs have two different answers: one for Android 3 and above (what I recommend to do) and another one for support from 2.1.
You have to define properly what is the lowest Android version you want to support, I recommend Android 4.0 (API 14): easy to implement and covers most devices of the market > 90%.
>3.0
Beginning with Android 3.0 (API level 11), the action bar is included in all activities
>2.1
The complicated point is that the ActionBarActivity (mentioned in the doc) is deprecated so you must use AppCompatActivity (docs). Therefore you need to include the android v7-appcompat library.
Normally, changing from FragmentActivity to AppCompatActivityshould not cause any trouble.
Extend your activity from AppCompatActivity use this line of code
in your .java class under set
setContentView(R.layout.activity_live);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
And
Use class="com.google.android.gms.maps.SupportMapFragment" in your
Fragment in .xml class as
<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"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pro.soft.inzi.tracky.Live" />
Note: must edit this fragment's last line and put you package name and class name instead of "com.pro.soft.inzi.tracky.Live"
I need to work back to API level 8. I have multiple activities and am using a navigation drawer to move between them as well as an action bar (with AppCompat v7). I have created a base abstract class that has all the navigation drawer setup and handling and that descends from ActionBarActivity.
File BaseNavActivity.java
abstract class BaseNavActivity extends ActionBarActivity {
}
All my activities subclass BaseNavActivity, and they all work fine.
public class MainActivity extends BaseNavActivity {
}
Before I added the navigation drawer feature I had a map working fine when descending from FragmentActivity. However, I cannot do that anymore because I need to descend from my BaseNavActivity. Even though the docs say ActionBarActivity descends from FragmentActivity I get an error when trying to use this XML.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background" >
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:orientation="vertical"
>
<include layout="#layout/common_toolbar" />
<fragment
android:id="#+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="3dip"
/>
<include layout="#layout/common_footer" />
</LinearLayout>
<!-- The navigation drawer -->
<include layout="#layout/nav_menu" />
</android.support.v4.widget.DrawerLayout>
I get an InflateException: Binary XML file line #19: Error inflating class fragment.
I have tried other solutions such as having a separate XML file with just the and opening it as a fragment and adding it to the above in place of the . That approach always returned a null when I tried to point to the map (smf was always null):
SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
I also tried to have a
public class MapFragment extends FragmentActivity {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.location_map, container, false);
return myFragmentView;
}
}
but I couldn't add the fragment using
MapFragment mapFragment = new MapFragment();
fragmentTransaction.add(R.id.map_container, mapFragment);
fragmentTransaction.commit();
The add() function needed to have parameters of (int, Fragment).
Any help will be appreciated. If you need more code I can add it but didn't want to make this post too long. I have spent a couple of days searching and trying various approaches and nothing seems to get everything working together.
Basically I would like to know how to have a <fragment> inside the XML when descending from a custom subclass that descends from ActionBarActivity. My 2nd choice would be to know how to add a fragment that holds the map and works with the support libraries.
Thanks!
I finally found my issue. I took a different approach for searching and found this gem: Android Google Maps V2 Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
Basically my issue was that I had the custom base class call setContentView before calling the super.onCreate. That meant that fragments weren't understood because it was a couple of supers up that knew about them. This caught me because 1) I needed to specify a theme that appcompat could use and had my setContentView next to it when I copied things to the base class, and 2) everything else worked up to this point so I didn't realize it was incorrect.
So the fix is in my base class that all my activities inherit from (abbreviated):
abstract class BaseNavActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState, int resLayout) {
// need to set the theme first
setTheme(R.style.AppTheme2);
// then call super before setting the content view
super.onCreate(savedInstanceState);
setContentView(resLayout);
}
}
Although I did everything it asks me to in the developers site
several times I can't get the map working anyhow.. I even tried to change computer and try again. When I try to launch the app it says gmap has unfortunately stopped working
Here is my MainActivity.java:``
public class MainActivity extends ActionBarActivity {
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
activity_main.xml:
<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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
When I check the logcat it says that there is error in main xml file where the fragment starts. Could you help me please.
Try to put all required permissions/api-key in your applications manifest file!
If you already did that post the whole exception here.
I hope you do close your RelativeLayout with </RelativeLayout>.
also, I used android:name="com.google.android.gms.maps.SupportMapFragment" instead of class="com.google.android.gms.maps.MapFragment" but your way could be perfectly fine too.
In my onCreate i did
SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
I never called .getMap as you did, and it works. Worth a try I'd guess.
I have a fragment activity as seen in my last question:
App crashing when using fragments
It crushes because of a GoogleMap inside one of my fragments (MainActivity).
right now thats the code:
private GoogleMap map;
map = ((MapFragment) getActivity().getFragmentManager() .findFragmentById(R.id.map)).getMap();
I cant change GoogleMap. it causes other problems in my code as im using it.
What should I do?
Thanks for your assistance!
Ive tried to relocate that piece of code to onCreateView. Doesn't work either.
Is there a way to implement mapview version 1 inside fragment?
I tried sth like:
MainActivity:
public class MainActivity extends FragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Then, I want 2 fragments . One that has the mapview and second which will have a textview in which the location will be shown.
I can't implement the first fragment.I can't figure how to implement the map.I mean , for the mapview to work it needs "extends MapActivity" but I need "extends fragment".
Also , in main.xml how to do it?I tried :
<fragment
class="com.google.android.maps.MapView"
android:name="com.example.....MapClass" //is this right?but it is necessary for fragment?
android:id="#+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
android:apiKey="mykey" />
<fragment
class="com.example...getloclass"
android:id="#+id/fragment2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
From here https://developers.google.com/maps/documentation/android/v1/hello-mapview, you can see that
Version 1 of the Google Maps Android API as been officially deprecated
as of December 3rd, 2012
So you shouldn't use MapView anymore. I suggest you use a MapFragment instead. See the excellent documentation for it here: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment