谷歌地图碎片崩溃

时间:2020-05-12 15:39:20

标签: android android-studio google-maps android-fragments

我想在片段中的Google地图上显示标记。我可以毫无问题地加载Google地图,但是当我运行mapFragment.getMapAsync(this)时,应用程序崩溃了,我也不知道为什么。有提示吗?

这是代码:

public class ISSLocation extends Fragment implements OnMapReadyCallback {    

    View v    
    GoogleMap mapAPI;
    SupportMapFragment mapFragment;
    private FragmentActivity myContext;


    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.activity_iss, container, false);

        FragmentManager fragManager = myContext.getSupportFragmentManager();

        mapFragment = (SupportMapFragment) fragManager.findFragmentById(R.id.mapAPI);
        mapFragment.getMapAsync(this);

        return v;
    }

 @Override
    public void onAttach(Activity activity) {
        myContext=(FragmentActivity) activity;
        super.onAttach(activity);
    }

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

        LatLng location = new LatLng(19.389137, 76.031094);
        mapAPI.addMarker(new MarkerOptions().position(location).title("Location"));
        mapAPI.moveCamera(CameraUpdateFactory.newLatLng(location));         
    }
}

这是布局xml代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:id="@+id/mapAPI"
        android:name="com.google.android.gms.maps.SupportMapFragment">

    </fragment>

</LinearLayout>

stacktrace: stacktrace

0 个答案:

没有答案