I'm creating programmatically a Huawei map, but despite MapFragment() is a child of Fragment class, the transaction add doesn't recognize it as a Fragment. Here's my code:
val transaction: FragmentTransaction = activity.supportFragmentManager.beginTransaction()
val mapFragment = MapFragment()
transaction.add(this.frame.id, mapFragment) ---> here is the problem
transaction.commit()
Someone knows the reason?
There are 2 different classes for showing map in fragment:
MapFragment
SupportMapFragment
You must use correct one your activity.
If Activity is just Activity and you use just FragmentManager - use MapFragment
If activity is AppCompatActivity and you use SupportFragmentManager - use SupportMapFragment
Related
Similiar to this this issue, i was trying to access and manipulate a second fragment that was created in the first fragment, before i realized that the nullpointerException is most probably caused by the nonready state of the second fragment. So is this generally not possible and i have to do something like passing a bundle with the data?
That works:
MyMap mapFragment = new MyMap();
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.frame,mapFragment,"map")
.commit();
but this gives me a null reference:
MyMap mapFragment = (MyMap)getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
Map fragment will be the default fragment and on a click a new fragment should be loaded and a value has to be entered in the fragment and this value has to be passed to the map fragment
To use MapFragment in Fragment Follow this link
using the Support Library to work (SupportMapFragment instead of MapFragment), so that the MapFragment can even work on below Android 3.0. You can replace the SupportMapFragment with the MapFragment if you are working on Android 3.0 or above.
I have an app that is extending ActionBarActivity and is using google maps as fragment. I obtain the map like this:
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = fm.getMap();
So my question is, how to add another fragment and how to switch between them ? I've been googling and searching for solution but found nothing yet.
Use FragmentTransaction.replace() to replace fragments. Example:
Fragment newFragment = new MyFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.commit();
For more check this answer too.
If you are using a support package (v4, v7 or v11) or you are targeting API< 14 use getSupportgetSupportFragmentManager(). Else, for API>=14 use getFragmentManager().
In my app, I only want to show the Google Map when a user taps a button. So, I created a subclass of SupportMapFragment and am now wanting to add that Fragment to my FragmentActivity using:
Fragment map = new ZSSGoogleMap();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.rootFrame, map);
fragmentTransaction.commit();
But, I keep getting an error on the first line telling me I need to cast it to ZSSGoogleMap.
What am I doing wrong here? I have worked with Fragments before and done exactly what I am trying to do here.
If you are using Fragments below API level 3.0 then you will have to use the Packages of v4.support for the Fragments and your Fragment will SupportFragment only.
As you are using SupportMapFragment so ,You will have to also use getSupportFragmentManager();
try this way if you want to use google map
MapFragment fragmentManager = getFragmentManager().findFragmentById(R.id.map);;
or if want to use support library then
SupportMapFragment fragmentManager = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap map= fragmentManager.getMap()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.rootFrame, map);
fragmentTransaction.commit();
In my android project, I would like to create a Google Map using java code rather than useing xml, how should I do?
You can add a Map using MapFragment to an Activity in code. To do this, create a new MapFragment instance, and then call FragmentTransaction.add() to add the Fragment to the current Activity.
mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.my_container, mMapFragment);
fragmentTransaction.commit();
For more details check out the GoogleMaps, you will have more idea of implementing the maps.