Java 无法解析getSupportFragmentManager()

Java 无法解析getSupportFragmentManager(),java,android,android-fragments,supportmapfragment,Java,Android,Android Fragments,Supportmapfragment,我在片段中使用了映射片段。但是在getSupportFragmentManager()上获取错误,即无法解析getSupportFragmentManager() 我尝试使用geChildFragmentManager(),因此它工作正常。但它在api 19上崩溃,它需要api 21及以上版本。我想为我的应用程序获取最低的api 我该怎么做 我也尝试使用支持库。但它仍在出错 有人能帮忙吗 碎片 import android.content.Intent; import android.os.Bu

我在片段中使用了映射片段。但是在
getSupportFragmentManager()
上获取错误,即无法解析
getSupportFragmentManager()

我尝试使用
geChildFragmentManager()
,因此它工作正常。但它在api 19上崩溃,它需要api 21及以上版本。我想为我的应用程序获取最低的api

我该怎么做

我也尝试使用支持库。但它仍在出错

有人能帮忙吗

碎片

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

}
布局

<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'
        }
    }
}
获取此异常

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)
更改:

 getSupportFragmentManager()
致:

您使用的是
android.support.v4.app.Fragment
,因此也要确保您的活动扩展。

,这将返回一个支持
FragmentManager

您无需执行任何特殊操作即可获得支持
FragmentManager

更改

mGoogleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();


Support library片段只有一个getFragmentManager(),它返回一个Support FragmentManager


您无需执行任何特殊操作即可获得支持FragmentManager。

使用
getActivity()。getSupportFragmentManager()
因为此方法在Fragment类中可用。我可以使用getChildFragmentManager,但它在api 19以上工作,所以我想在api 19以下工作我的应用程序。试图使用getActivity,但它仍会因空指针异常而崩溃…@ρцσѕρєKUsing
getChildFragmentManager()
是为嵌套的SupperOtmapFragment执行此操作的正确方法。看看这个答案中的工作代码(第一个示例):但它在api 21下不起作用@api-17及以上版本上的Daniel NugentNative片段支持
getChildFragmentManager()
。如果您使用支持库片段,那么它也可以在较低的api级别上工作。请参见此处,了解api级别17中添加的本机片段:感谢您的回复。我想问,支持库在依赖项中是否正确??如果我使用的是片段活动,我添加了widget android.support.v7.widget.Toolbar,因此它在setSupportActionBar(Toolbar)上出现错误@马辛·Jędrzejewski@user5669913如果您想使用setSupportActionBar(和ActionBar),那么请使用
android.support.v7.app.AppCompatActivity
,它扩展了
android.support.v4.app.FragmentActivity
是,因此我使用了AppCompatActivity,并尝试使用getActivity.getSupportFragmentManager()访问地图尽管如此,它还是在这一行上与NullPointerException一起崩溃mGoogleMap=((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();您是否检查了它的
getSupportFragmentManager()
是否返回null,而不是
findFragmentById(R.id.map)
?是的,映射的id是正确的。因为如果我使用getChildFragmentManager(),它将在api 21上运行。感谢您的回复。。你的意思是我应该使用getSupportFragmentManager??如果我正在使用该获取错误。。不可转换类型错误无法将android.support.v4.app.fragment强制转换为com.google.android.gms.maps.MapsFragment@Tanis.7x
 getActivity().getSupportFragmentManager()
mGoogleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();
mGoogleMap = ((SupportMapFragment)**getActivity()**.getSupportFragmentManager().
   findFragmentById(R.id.map)).getMap();