【发布时间】:2016-07-27 12:23:17
【问题描述】:
我对 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();
}
我在构建时的错误信息是 Error:(77, 25) error: incompatible types: MapShopsFragment cannot be convert to 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 与 play-services: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;
......
}
}
【问题讨论】:
标签: android android-studio android-fragments fragmentmanager