I'm developing an app which uses fragment tab, one of my fragment uses Google Maps V2 "SupportMapFragment"
public class A extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = null;
view = inflater.inflate(R.layout.all_coffee_shops_view, container, false);
map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
}
my xml:
<RelativeLayout
android:id="#+id/map_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
I'm calling B Fragment from this A fragment(A->B) and when I come back from B->, A fragment onCreateView throws an exception, "android.view.InflateException: Binary XML file line #132: Error inflating class fragment"
when once map fragment is created and when comes back to this fragment again fragment is onCreateView() is called that's why you are getting force close
Try this once in your fragment containing map
#Override
public void onDestroyView() {
super.onDestroyView();
Fragment f = getFragmentManager().findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
please comment me about the result
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView");
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}
Related
I have a map fragment class and I added a button on top of it. When I click on it I am calling another fragment class with a simple textview and save button. When save is clicked I want to go back to my mapfragment class. Right now it is throwing an error when I click save and go back. The error is when it is inflating the class > Just wondering if I am doing anything wrong while popping from the backstack. I just want to remove the last one and go back to my map.
My map layout is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</fragment>
</LinearLayout>
MapFragment is as follows:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.maplayout,container,false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (MapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = MapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}
The back method:
FragmentManager fm = getActivity().getFragmentManager();
fm.popBackStack("TEXT_FRAGMENT", FragmentManager.POP_BACK_STACK_INCLUSIVE);
fm.beginTransaction()
.replace(R.id.map_tab,
mapFragment,
"maptab")
.addToBackStack("maptab")
.commit();
The error is: Fragment does not have a view.
The map_tab is the tab on which MapFragment was added. The map fragment is added on top of this.
EDIT: In the back method I added fm.popBackStack("TEXT_FRAGMENT", FragmentManager.POP_BACK_STACK_INCLUSIVE
but then this gives me the error when I go back to my Map Fragment and on inflating I get the error.
Don't commit a new Fragment. Instead,
Look at the getFragmentManager().popBackStack() methods (there are several to choose from)
http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()
Got it to work finally. Had to remove the mapfragment before adding a new fragment.
I`m developing an application that uses an activity with navigation drawer. I have two fragments in the drawer one of which is a google map.
In the map fragment class I want to initialize the GoogleMap object to do some things to the map, but in the onCreateView after I initialize the view through inflater, my app doesn't find the id in R class to initialize the google map.
I call the fragment from the activity through:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.mainContent, map, Map.class.getSimpleName());
fragmentTransaction.addToBackStack(Map.class.getSimpleName());
fragmentTransaction.commit();
the container xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawerList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/background_color" >
</ListView>
</android.support.v4.widget.DrawerLayout>
layout_fragment_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
The Map fragment class:
public class Map extends Fragment {
private View view;
private MapFragment mapFragment;
private GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.layout_fragment_map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
return view;
}
In the line:
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
the R.id.map is undefined Eclipse gives "could not resolve type".
Something is missing or my whole concept is faulty?
Thank you and happy coding!
I resolved the issue by modifying the line:
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.googleMap);
with
mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);
It is strange because the R.id.googleMap is still undefined but the object gets created correctly.
i have a mapfragment inside another fragment
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
/>
</FrameLayout>
and im initializing map in oncreateview
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.event_fragment, null);
try{
events = new ArrayList<EventsModel>();
try {
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
double latitude = 24.875419;
double longitude = 66.993293;
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(12).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
loadEvents().execute();
}catch(Exception e){
}
return view;
}
its working fine but when i press back button and reopen this fragment it crashes
04-19 21:18:21.803: E/AndroidRuntime(31612): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f050006, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
then i googled it and put it in ondestroy
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
the above problem is resolved bu now it gives me nullpointer on reopening the fragment that had mapfragment in it may be because i removed it with above code but whats the solution now?
Please make sure that you have correctly checked the condition where map inflated is null or not while creating view for map fragment or while destroying.
put this one on onDestroy() method
SupportMapFragment mapFragment = ((SupportMapFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.map_location_sharing));
if(mapFragment != null) {
FragmentManager fM = getFragmentManager();
fM.beginTransaction().remove(mapFragment).commit();
Im trying put a MapFragment inside a DialogFragment, it works fine but i dont know how can I wait until getMap()!=null... hereĀ“s my code:
public class DialogMapa extends DialogFragment{
private SupportMapFragment fragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_dialog_mapa, container,false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
SupportMapFragment fragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map, fragment).commit();
GoogleMap mapa = null;
try {
mapa =fragment.getMap();
} catch (Exception e) {
e.printStackTrace();
}
if (mapa==null) Toast.makeText(getActivity(), "NULL", Toast.LENGTH_SHORT).show();
return view;
}
}
and my inflate layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<FrameLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
Thanks in advance!!
So with the new maps framework, that call is not guaranteed to return non null. The map is pretty complex and is initializing a lot of state on and off the render thread.
In your case it's safe to call after onFragmentsResumed() or after the fragment is attached to an activity. You could always use a handler to check again some point later.
getMap() is not null inside onStart() of the dialog fragment, after view created.
I'm trying to load a map with com.google.android.gms.maps.SupportMapFragment, but I get null in findFragmentById.
My xml fragment file:
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
MainActivity.java:
public class MainActivity extends ActionBarActivity{
private void selectItem(int position){
//some code
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new MyMapFragment).commit();
}
}
MapFragment:
MyMapFragment extends android.support.v4.app.Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
SupportMapFragment smf = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
//here smf is null
}
}
This is the answer:
Note: You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.
And furthermore when playing with nested fragments, use getChildFragmentManager().