1

I am using map fragment inside fragment. But getting error on getSupportFragmentManager() i.e. can not resolve getSupportFragmentManager().

I tried to use geChildFragmentManager() so it was working fine. But it crashes on api 19 it needs api 21 and above. I want to get the lowest api for my app.

How can I do this??

I tried to use support library too. Still it is giving error..

Can anyone help please..?

Fragment

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

public class GoFoodNearMeFragment extends Fragment {

    private LocationRequest mLocationRequest;
    private GoogleApiClient mGoogleApiClient;
    boolean mUpdatesRequested = false;
    private GoogleMap mGoogleMap;
    private Toolbar toolbar;
    private Intent i;
    private GPSTracker gps;
    private LatLng curentpoint;
    double latitude;
    double longitude;
    private RelativeLayout suggest;
    private RelativeLayout search;

    public GoFoodNearMeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_go_food_near_me, container, false);
        mGoogleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();

        mGoogleMap.setMyLocationEnabled(true);
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
        mGoogleMap.getUiSettings().setCompassEnabled(true);
        mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
        mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);

        gps = new GPSTracker(getActivity());

        gps.canGetLocation();

        latitude = gps.getLatitude();
        longitude = gps.getLongitude();
        curentpoint = new LatLng(latitude, longitude);

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(curentpoint).zoom(19f).tilt(70).build();

        mGoogleMap.setMyLocationEnabled(true);
        mGoogleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));

        suggest= (RelativeLayout)v.findViewById(R.id.suggest);
        search =(RelativeLayout)v.findViewById(R.id.LinearSearch);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i = new Intent(getActivity(),SearchRestaurantsActivity.class);
                startActivity(i);

            }
        });
        suggest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i=new Intent(getActivity(),SuggestRestaurantActivity.class);
                startActivity(i);
            }
        });
        return v;
        }

}

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:fitsSystemWindows="true">


    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:background="@android:color/white"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="65dp"
        android:id="@+id/LinearSearch"
        android:layout_below="@+id/toolbar">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/search"
            android:id="@+id/textView12"
            android:layout_gravity="center"
            android:layout_centerInParent="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView29"
            android:layout_gravity="center"
            android:background="@drawable/ic_search_black_24dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp" />

    </RelativeLayout>

        <fragment android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/map"
            tools:context=".GoFoodNearMeFragment"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_below="@+id/LinearSearch"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="05dp" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:background="@color/black_trans80"
        android:layout_gravity="bottom"
        android:id="@+id/suggest"
        android:layout_alignParentBottom="true">


        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:id="@+id/imageView35"
            android:layout_gravity="center"
            android:background="@drawable/lightbulb14"
            android:layout_alignParentTop="false"
            android:layout_toStartOf="@+id/textView13"
            android:layout_alignParentBottom="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/suggestRest"
            android:id="@+id/textView13"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:layout_marginLeft="05dp" />

    </RelativeLayout>



</RelativeLayout>


dependencies

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.1'

    defaultConfig {
        applicationId "com.example.siddhi.go_jek"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies

 {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.google.android.gms:play-services:8.3.0'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.google.android.gms:play-services-ads:8.3.0'
    compile 'com.google.android.gms:play-services-identity:8.3.0'
    compile 'com.google.android.gms:play-services-gcm:8.3.0'
    compile 'info.hoang8f:android-segmented:1.0.6'
    compile 'com.android.support:support-v4:23.1.0'
}

getting this exception

FATAL EXCEPTION: main
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime: Process: com.example.siddhi.go_jek, PID: 10525
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime: java.lang.NullPointerException
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime:     at com.example.siddhi.go_jek.GoFoodNearMeFragment.onCreateView(GoFoodNearMeFragment.java:53)
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
  • Use `getActivity(). getSupportFragmentManager()` because method is available `FragmentActivity` instead of `Fragment` class or you can also use `getChildFragmentManager()` which you can access without using `getActivity()` because this method is available in Fragment class.Thanks – ρяσѕρєя K Dec 23 '15 at 16:15
  • Its working with getChildFragmentManager but it works above api 19 I want to work my app below 19 api too. Tried to use getActivity still it crashes with null pointer exception...@ρяσѕρєя K –  Dec 23 '15 at 16:41
  • Using `getChildFragmentManager()` is the correct way to do it for a nested SupporotMapFragment. Take a look at the working code in this answer (the first example): http://stackoverflow.com/a/32579020/4409409 – Daniel Nugent Dec 23 '15 at 17:14
  • 1
    but it dosent work below api 21.. @Daniel Nugent –  Dec 23 '15 at 17:15
  • Native Fragments on api-17 and up support `getChildFragmentManager()`. If you use support library Fragments then it will work on lower api levels as well. See here, for Native Fragments, added in api level 17: http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager() – Daniel Nugent Dec 23 '15 at 17:17
  • One more thing, that GPSTracker class is not that great. For an example of using FusedLocationProviderApi to get location for a map, take a look here: http://stackoverflow.com/questions/30253123/blue-dot-and-circle-is-not-shown-on-mylocation-using-android-fused-location-api – Daniel Nugent Dec 23 '15 at 17:25
  • Thank you. But can you please show me the code, which changes I have to do to get it work with getChildFragmentManager() on lower api too..@ Daniel Nugent –  Dec 23 '15 at 17:36
  • mGoogleMap =((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); I used this .Its crashing with null pointer exception on api 19 .. please help. @@ Daniel Nugent –  Dec 23 '15 at 18:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98845/discussion-between-daniel-nugent-and-user5669913). – Daniel Nugent Dec 23 '15 at 18:48

3 Answers3

1

Change:

 getSupportFragmentManager()

to:

 getActivity().getSupportFragmentManager()

You are using android.support.v4.app.Fragment so also make sure your Activity extends FragmentActivity.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Thank you for reply. I wanted to ask, Is the support library is correct in dependencies?? If I am using fragment activity, I have added widget android.support.v7.widget.Toolbar so it is giving error on setSupportActionBar(toolbar); @Marcin Jędrzejewski –  Dec 23 '15 at 16:26
  • 1
    @user5669913 If you want to use setSupportActionBar (and ActionBar) then use `android.support.v7.app.AppCompatActivity`, it extends `android.support.v4.app.FragmentActivity` – marcinj Dec 23 '15 at 16:32
  • 1
    Yes so I have use AppCompatActivity and tried to access map wit getActivity.getSupportFragmentManager() still it crashes with nullPointerxception on this line mGoogleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById( R.id.map)).getMap(); –  Dec 23 '15 at 16:39
  • 1
    Have you checked whether its `getSupportFragmentManager()` returning null, and not `findFragmentById( R.id.map)`? – marcinj Dec 23 '15 at 16:41
  • yes the id for map is correct. Because it runs on api 21 if i am using getChildFragmentManager(). –  Dec 23 '15 at 16:43
  • add exception from logcat to your question, this will help find which method actually returns null. – marcinj Dec 23 '15 at 16:49
  • @user5669913: but `getChildFragmentManager ` method is available in support lib – ρяσѕρєя K Dec 23 '15 at 16:55
  • its in android.support.v4.app.Fragment :http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager() and you extend from this class – marcinj Dec 23 '15 at 17:01
  • Please check I have added exception to my question..@ Marcin Jędrzejewski –  Dec 23 '15 at 17:06
0

Support library Fragments only have a getFragmentManager(), which returns a support FragmentManager.

You do not need to do anything special to get a support FragmentManager.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Thank you for reply.. U mean I should use getSupportFragmentManager?? If I am using that getting error.. inconvertible types error can not cast android.support.v4.app.fragment to com.google.android.gms.maps.MapsFragment. @Tanis.7x –  Dec 23 '15 at 16:34
0

Support library Fragments only have a getFragmentManager(), which returns a support FragmentManager.

You do not need to do anything special to get a support FragmentManager.