本文介绍了使用导航抽屉在片段中创建Google Maps v2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Fragment 中创建地图,但一直出现此错误:

I am trying to create a map in a Fragment but keep getting this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
        at com.new.newapps.activity.MapsFragment.initilizeMap(MapsFragment.java:41)
        at com.new.newapps.activity.MapsFragment.onResume(MapsFragment.java:52)

我不确定如何解决此问题.请在下面查看我的代码.

I am not sure how to fix this. Please have a look at my code below.

MapsFragment类:

MapsFragment class:

public class MapsFragment extends Fragment {

public MapsFragment(){}
GoogleMap googleMap;

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

    View rootView = inflater.inflate(R.layout.fragment_distance, container, false);
    try {
        initilizeMap();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootView;
}

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map))
                .getMap();
        if (googleMap == null) {
            Toast.makeText(getActivity(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}
@Override
public void onResume() {
    super.onResume();
    initilizeMap();
    if (googleMap != null) {

    }
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(xx.xxxx, xx.xxxx))
            .zoom(10)
            .bearing(0)
            .tilt(80)
            .build();

    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}
}

然后在我的布局文件中:

Then in my layout file:

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

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>


</RelativeLayout>

这是我的gradle文件:

And this is my gradle file:

compile 'com.google.android.gms:play-services:6.5.87'

对不起,我的英语不好:-)

Excuse my bad English :-)

任何帮助将不胜感激!谢谢

Any help will be appreciated! Thanks

推荐答案

我认为您犯了小错误,请查看OnResume方法:

You have did small mistake i think, Check out OnResume Method:

@Override
public void onResume() {
    super.onResume();
    initilizeMap();
    if (googleMap != null) {

    }
}

将此更改为

@Override
public void onResume() {
    super.onResume();

    if (googleMap != null) {
        initilizeMap();
    }
}

这篇关于使用导航抽屉在片段中创建Google Maps v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:06