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();
Related
I am using PlaceAutoCompleteFragment inside a Fragment. First time when Fragment(App fragment in which PlaceAutoCompleteFragment is placed) opens it works fine like a charm.
But, then second time I hit button to open Fragment it crashes with below error. It works only a single time.
FATAL EXCEPTION:
android.view.InflateException: Binary XML file line #64: Error
inflating class fragment Caused by:
java.lang.IllegalArgumentException: Binary XML file line #64:
Duplicate id 0x7f0d0094, tag null, or parent id 0xffffffff with
another fragment for
com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
This is how I am using this,for Search location
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.parkingview_fragment, container, false);
mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map_parkingview);
mapFragment.getMapAsync(this);
// For Search location
PlaceAutocompleteFragment autocompleteFragment = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_fragment_parkingview);
}
autocompleteFragment.setOnPlaceSelectedListener(this);
autocompleteFragment.setHint("Search a Location");
// autocompleteFragment.setBoundsBias(BOUNDS_MOUNTAIN_VIEW);
return view;
}
XML:
<fragment
android:id="#+id/place_fragment_parkingview"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Declare your fragment Dynamically it will solve the issue.
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
And Update your fragment onCreateView
SupportPlaceAutocompleteFragment autocompleteFragment = new SupportPlaceAutocompleteFragment();
android.support.v4.app.FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, autocompleteFragment);
ft.commit();
This problem is caused because you haven't removed yet the PlaceAutoCompleteFragment instance created before at some point.
Careful guys! If you use the method posted by Jordon, it will work, but the fragment's views (the input edittext, and the clearbutton for example) won't be accesible because the fragment's views aren't created yet...the correct answer is REMOVE the instance of the fragment putted in the xml file, programmatically, overwriting the onDestroyView method on the Fragment containing the PlaceAutoCompleteFragment.
#Override
public void onDestroyView() {
if (autocompleteFragment != null) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.remove(autocompleteFragment).commitNowAllowingStateLoss();
}
super.onDestroyView();
}
This way, anytime you recreate your Fragment it won't have an instance of the PlaceAutoCompleteFragment already created. Happy Coding!
I was using-
<fragment
android:id="#+id/location_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/plain_border"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />
inside my fragment layout file and initializing it using-
PlaceAutocompleteFragment locationAutocompleteFragment = (PlaceAutocompleteFragment)
getActivity().getFragmentManager()
.findFragmentById(R.id.location_autocomplete_fragment);
I switched to-
<fragment
android:id="#+id/location_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/plain_border"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment" />
and initialisation by-
SupportPlaceAutocompleteFragment locationAutocompleteFragment = (SupportPlaceAutocompleteFragment)
getChildFragmentManager()
.findFragmentById(R.id.location_autocomplete_fragment);
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.
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 have the following code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_left, null);
// test
SupportMapFragment fragment = new SupportMapFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.map, fragment).commit();
GoogleMap googleMap = fragment.getMap();
if(getCameraPosition() == null) { // first time in app
setCameraPosition(new CameraPosition(getYourLatLng(), 12, 0 ,0));
}
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(getCameraPosition()));
googleMap.addMarker(new MarkerOptions()
.title("Your Position")
.snippet("This is where your smartphone is located.")
.position(getYourLatLng()));
return view;
}
This is what I'm trying to replace in the xml file:
<FrameLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Whats up with this? Anyway to update the id?
NEW Stack Trace:
10-29 22:37:52.277: E/AndroidRuntime(16273): FATAL EXCEPTION: main
10-29 22:37:52.277: E/AndroidRuntime(16273): java.lang.NullPointerException: CameraUpdateFactory is not initialized
10-29 22:37:52.277: E/AndroidRuntime(16273): at com.google.android.gms.internal.s.b(Unknown Source)
10-29 22:37:52.277: E/AndroidRuntime(16273): at com.google.android.gms.maps.CameraUpdateFactory.aX(Unknown Source)
You need to set a MapReadyCallback.
https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment
When the map is ready, you can move the Camera. (There is a function providing a GoogleMap that allow you to move camera.)
https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback.html
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();
}
}