使用Fragment时不会触发OnMapReadyCallback

时间:2016-06-13 07:29:41

标签: android android-fragments

永远不会调用 onMapReady 函数。需要触发它以正确初始化我的GoogleMap。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;

public class MapsFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;

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

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

我应该继承 MapFragment 而不是片段吗?因为当我这样做时我得到另一个错误:它在 MainActivity

中返回NullPointerException
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name=".fragment.MapsFragment"
        android:id="@+id/maps_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </fragment>
</LinearLayout>

main_activity.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.MapsFragment" />

activity_maps.xml

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);
}

非常感谢你们!

更新1: 将此添加到 MapsActivity ,但现在 MainActivity 会返回android.view.InflateException:二进制XML文件行#6:错误导致类片段

{{1}}

3 个答案:

答案 0 :(得分:1)

您的MapsFragment应该请求 getMapAsync 加载地图,然后才会调用 onMapReady

public class MapsFragment extends Fragment implements OnMapReadyCallback
{
    private View fragmentView;
    private GoogleMap mMap;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        fragmentView = inflater.inflate(R.layout.activity_maps, container, false);
        return fragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


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

答案 1 :(得分:0)

试试这个

public class MapsFragment extends Fragment implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    View mView = inflater.inflate(R.layout.activity_maps, container, false);

    SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));

    mapFragment.getMapAsync(this);
    return mView ;

}

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

答案 2 :(得分:0)

这是正确的工作方案:

<强> MapsFragment

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

public class MapsFragment extends SupportMapFragment {

    private GoogleMap mapView;

    private void initGoogleMap() {
        getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mapView = googleMap;
            }
        });

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initGoogleMap();
    }

}

<强> MainActivity

public class MainActivity extends AppCompatActivity {

    private MapsFragment mapsFragment;

    private void initializeMapsFragment() {
        FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
        mapsFragment = new MapsFragment();
        SupportMapFragment supportMapFragment = mapsFragment;
        mTransaction.add(R.id.google_maps_frame, supportMapFragment);
        mTransaction.commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        initializeMapsFragment();
    }
}

main_activity.xml ,删除 maps_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/google_maps_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="-8dp"
        android:layout_weight="0.2" >
    </FrameLayout>

</LinearLayout>