Reputation:
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)
Upvotes: 1
Views: 2142
Reputation: 241
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.
Upvotes: 0
Reputation: 67229
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
.
Upvotes: 0
Reputation: 50016
Change:
getSupportFragmentManager()
to:
getActivity().getSupportFragmentManager()
You are using android.support.v4.app.Fragment
so also make sure your Activity extends FragmentActivity.
Upvotes: 1