Fragmentmanager第二个变量

时间:2016-07-27 12:23:18

标签: android android-studio android-fragments fragmentmanager

我有FragmentManager的问题。我想在片段中获取谷歌地图。这是我的片段。我想使用com.google.android.gms制作给定位置的地图:play-services:9.2.1

>

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.spoturdeal.spoturdeal.R;


public class MapShopsFragment extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapdeals);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Somewhere, and move the camera.
        LatLng somewhere = new LatLng(51, 6);
        mMap.addMarker(new MarkerOptions().position(somewhere).title("Marker in somewhere"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(somewhere));
    }
}

MainActivity具有以下导入

>

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import android.Manifest;


import com.spoturdeal.spoturdeal.R;
import com.spoturdeal.spoturdeal.fragments.MapShopsFragment;

我已经检查过我使用的是android.support.v4.app.FragmentActivity 有问题的代码是这部分

>

private void displayInitialFragment() {

        MapShopsFragment mapShopsFragment = new MapShopsFragment();
        FragmentManager fragmentmanager = getSupportFragmentManager();
        fragmentmanager.beginTransaction()
                .setCustomAnimations(R.anim.anim_slide_in_from_left, R.anim.anim_slide_out_from_left)
                .replace(R.id.relativelayout_for_fragment,
                       mapShopsFragment,          /** here is the Problem
                       mapShopsfragment.getTag() 
                ).commit();

    }

构建时可以出现的错误消息是错误:(77,25)错误:不兼容的类型:MapShopsFragment无法转换为Fragment

我还有几个我正在处理的碎片他们有问题。它们扩展为ListFragments。

我不想在另一个Activity中创建地图,因为我必须将Material Design菜单设置为它。

在android studio的状态行,我收到以下消息

  'android.support.v4.app.FragmentTransaction'中的

'replace(int,android.support.v4.app.Fragment)'无法应用于'(int,com.spoturdeal.spoturdeal.fragments.MapShopsFragment)'但不明白它是什么意思。

我能够在@ben的帮助下使Fragment工作以使Map使用播放服务:9.2.1有点难度。以下是完美运行的解决方案。

>

public class MapShopsFragment extends Fragment implements OnMapReadyCallback {


    private MapView mapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                Bundle savedInstanceState) {
        return inflater.inflate(R.layout.mapdeals, container, false);

    }
    public void  onViewCreated(View v, Bundle savedInstanceState) {

        mapView = (MapView) v.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        mapView.onResume();
        mapView.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;
        ......
    }
}

1 个答案:

答案 0 :(得分:0)

问题是你创建了FragmentActivity而不是Fragment。片段管理器希望您传递支持片段。对于你正在做的工作,MapShopsFragment需要扩展Fragment。