Accessing dynamically added google map fragment in framelayout - android

I have dynamically added my google map fragment in framelayout. Now I want to put markers and do other stuff with google map. but i am not able to access that through get fragment manager. The code shows no error but when it executes it returns null pointer exception.
I thinks its logical error. Something to do with type casting. plz help
Here is my "nearby_places.xml" layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.localiteproximus.CustomAutoCompleteTextView
android:id="#+id/atv_places"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="#string/str_atv_places"
android:singleLine="true" />
<FrameLayout
android:id="#+id/mapView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="#id/atv_places">
</FrameLayout>
</RelativeLayout>
and this is my class
public class NearByPlaceFragment extends Fragment {
public NearByPlaceFragment(){}
View rootView;
GoogleMap googleMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MapFragment fragment = new MapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapView, fragment).commit();
rootView = inflater.inflate(R.layout.nearby_places, container, false);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
return rootView
}
and LogCat is showing this
58.608: E/AndroidRuntime(12654): FATAL EXCEPTION: main
03-22 03:00:58.608: E/AndroidRuntime(12654): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.localiteproximus/com.localiteproximus.NearbyPlaces}: android.view.InflateException: Binary XML file line #21: Class is not a View com.google.android.gms.maps.MapFragment
03-22 03:00:58.608: E/AndroidRuntime(12654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
03-22 03:00:58.608: E/AndroidRuntime(12654): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.view.View
03-22 03:00:58.608: E/AndroidRuntime(12654): at java.lang.Class.asSubclass(Class.java:1182)
03-22 03:00:58.608: E/AndroidRuntime(12654): at android.view.LayoutInflater.createView(LayoutInflater.java:565)
03-22 03:00:58.608: E/AndroidRuntime(12654): ... 23 more

I got answer on my own.
So it was really silly mistake.. after 6 hours i found that the error was on this line
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();
which was getting its parent view instead of child so replaced it with this line
googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.mapView)).getMap();
and everything is fine :).

Related

How to use PlaceAutoCompleteFragment widget in a Fragment

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);

Android application crashes on trying to inflate map layout on some devices but works fine on latest devices

This is my layout file:
My xml file which contains the map layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" 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"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</FrameLayout>
My FragmentClass
public class RouteFragment extends Fragment {
GoogleMap map;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_layout,container,false);
// Getting reference to SupportMapFragment of the activity_main
SupportMapFragment fm = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
// Getting Map for the SupportMapFragment
map = fm.getMap();
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(MainActivity.lng1);
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
map.addMarker(options);
options.position(MainActivity.lng2);
map.addMarker(options);
map.moveCamera(CameraUpdateFactory.newLatLng(MainActivity.lng1));
map.animateCamera(CameraUpdateFactory.zoomTo(8));
if(getArguments().getInt("Driving")==1)
map.addPolyline(MainActivity.lineOptions);
else {
Polyline line = map.addPolyline(new PolylineOptions()
.add(MainActivity.lng1, MainActivity.lng2)
.width(5)
.color(Color.RED));
}
return v;
}
}
Here is the exception:
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
Manifest file:
I have requested the desired permissions and defined the uses-library for incroporating maps in the application.
<uses-library android:name="com.google.android.maps"/>
<permission
android:name="com.city.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.city.permission.MAPS_RECEIVE" />

InflateException error when nesting Fragments within Fragments

I have a Fragment, which contains two other Fragments, namely a ListFragment and a regular Fragment. When I inflate the parent Fragment I receive an InflateException runtime error.
This is due to like #11 in my XML layout code which refers to this:
<fragment
android:id="#+id/fragment1"
android:name="com.nanospark.TMS.fragment_profile_list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/new_profile_button" />
Keep in mind this XML above refers a nested Fragment.
Is nesting Fragments a supported feature in Android? Are there any alternatives or fixes to this issue?
Logcat:
04-17 15:00:58.198: E/AndroidRuntime(1400): FATAL EXCEPTION: main
04-17 15:00:58.198: E/AndroidRuntime(1400): android.view.InflateException: Binary XML file line #11: Error inflating class fragment
EDIT 1:
From what I've discovered it is supported
http://developer.android.com/about/versions/android-4.2.html#NestedFragments
However I cannot find any good examples of nested Fragments and filling parent fragments with them.
You can add it in your fragment code:
public class YourFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, null);
if (getChildFragmentManager().findFragmentByTag("main") == null)
{
if (getActivity() == null)
return null;
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
NewsMainFragment nf = (YourNested) YourNestedFragementClass.instantiate(getActivity(), YourNestedFragementClass.class.getName(),savedInstanceState);
ft.add(R.id.fragment_container, nf,"main");
ft.commit();
}
return v;
}
}

Null pointer exception using maps API2 android?

I am integrating maps in my application.
The map is also shown in the application.
But I am not able to find its view.
This is my fragment class.
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.search_fragment,
container, false);
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
// googleMap.setMyLocationEnabled(true);
// googleMap.getUiSettings().setZoomControlsEnabled(false);
return rootView;
}
}
I am getting the null pointer exception..
Log cat is as follows:
FATAL EXCEPTION: main
: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.herobike.main/com.herobike.service.SearchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4803)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.herobike.service.SearchActivity$PlaceholderFragment.onCreateView(SearchActivity.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
at android.app.Activity.performStart(Activity.java:5018)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
Line No: 52 is
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
This is xml of fragment
<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"
android:background="#drawable/sc_10"
tools:context="com.herobike.main.SearchActivity$PlaceholderFragment" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="2dp" />
What needs to be updated here?
In your layout file instead of MapFragment you need to write SupportMapFragment as you have already accessed it using SupportMapFragment .So change it as below:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="2dp" />
Also initialize your map as below:
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
Check if you have referenced the id of fragment right.
public final GoogleMap getMap ()
Gets the underlying GoogleMap that is tied to the view wrapped by this fragment.
Returns
the GoogleMap. Null if the view of the fragment is not yet ready.
This can happen if the fragment lifecyle have not gone through
onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also
happen if Google Play services is not available. If Google Play
services becomes available afterwards and the fragment have gone
through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this
method again will initialize and return the GoogleMap.
It is better to chekc the availability of google play services before initializing map object.
Edit:
yes inside fragment
What you have done
android:name="com.google.android.gms.maps.MapFragment"
Its a MapFragment.
But you have
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
which is wrong
What you need to do
So you want map inside a fragment. So you need to extend MapFragment instead of Fragment or you should use MapView
Check the below
Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment
pLease try this ,let me know if any concern
if (googleMap == null)
{
googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_history)).getMap();
}
try this
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
and make the following changes in your layout
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
use supportmapfragment :-
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="250dp"
class="com.google.android.gms.maps.SupportMapFragment" />

Fragment inside Fragment error

I was changing a Activity to a Fragment to use inside a Scrollable Tab Activity. But I getting this exception I load this fragment:
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.zhensydow.demo.MainMenuFragment.onCreateView(MainMenuFragment.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
....
And the used (simplified) xml loaded is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<fragment <<---- LINE #17
android:id="#+id/mlist"
android:name="com.zhensydow.demo.MListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#android:layout/mcontent" />
<FrameLayout ... />
</LinearLayout>
The error is caused in this code:
public class MainMenuFragment extends Fragment implements
MenuListFragment.Callbacks {
// ...
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
LinearLayout ll = (LinearLayout) inflater.inflate(
R.layout.activity_main_menu, container, false);
return ll;
}
// ...
}
The error is on a fragment, I think it's caused because the old activity loaded two fragments inside him.
How can I solve it?
UPDATE: added full fragmen xml data
I suspect your Fragment contains some code like this one, requiring your Activity to implement a callback interface, and your Activity does not implement that interface.
A fragment tag is required to have both a name and an id.

Categories

Resources