getSupportFragmentManager()返回null?

时间:2017-12-26 10:21:34

标签: android android-fragments google-maps-api-3

我将谷歌地图实施到我的Android应用程序中,但我无法在我的activity.java中配置地图.getSupportFragmentManager返回空值。

这是我的pacmain_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    
    <include layout="@layout/toolbar"/>   
    <fragment
        android:id="@+id/pacientemap"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />    
</RelativeLayout>

PacienteMainActivty.java

public class PacienteMainActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback{

    private PacMainPresenterImpl pacMainPresenterImpl;
    private LocationManager locationManager;
    private SupportMapFragment map;
    private String provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.pacientemap);
        mapFragment.getMapAsync(this);

3 个答案:

答案 0 :(得分:4)

setContentView 缺少添加  {/ 1}}高于此

setContentView(R.layout.pacmain_activity);

最终代码

SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.pacientemap);
        mapFragment.getMapAsync(this);

答案 1 :(得分:3)

添加setContentView(R.layout.pacmain_activity);

没有它你什么都没有

旧问题:getSupportFragmentManager returns null

答案 2 :(得分:0)

在从布局中查找内容之前,您必须使用活动添加布局。您必须添加setContentView(R.layout.pacmain_activity);您的最终代码

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