Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing dynamically added google map fragment in framelayout

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
like image 239
Junaid Avatar asked Mar 20 '23 03:03

Junaid


1 Answers

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 :).

like image 54
Junaid Avatar answered Apr 20 '23 00:04

Junaid