无法解析getSupportFragmentManager()

时间:2015-12-23 16:14:48

标签: java android android-fragments supportmapfragment

我在片段内使用地图片段。但是在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'
        }
    }
}

依赖

 {
    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'
}

获得此异常

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)

4 个答案:

答案 0 :(得分:1)

变化:

 getActivity().getSupportFragmentManager()

为:

android.support.v4.app.Fragment

您正在使用SELECT JotID FROM uv_attributes WHERE HOC = 1 AND JotTypeID = 5 GROUP BY JotID HAVING (MAX(CASE WHEN city = 'London' AND language = 'English' THEN 1 ELSE 0 END) = 1 AND MAX(CASE WHEN city = 'San Francisco' AND language = 'English' THEN 1 ELSE 0 END) = 1) OR MAX(CASE WHEN city = 'Madrid' AND language = 'Spanish' THEN 1 ELSE 0 END) = 1 ,因此请确保您的活动延伸FragmentActivity

答案 1 :(得分:0)

Support library Fragments only have a getFragmentManager(),返回支持if (preg_match_all( $pattern, $subject, $matches)) { var_dump($matches); }

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

答案 2 :(得分:0)

更改

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

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

答案 3 :(得分:0)

支持库片段只有一个getFragmentManager(),它返回一个支持FragmentManager。

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