3

我在 Google Map Fragments 中遇到了一些问题“getmap() 处的空指针异常”。我想在片段中插入 mapfragment 请查看代码并告知我是否采用了正确的方法......并让我知道我需要对我的代码做什么才能使其正常工作

我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  android:background="@drawable/bg2"
  android:weightSum="100">

<Button
    android:id="@+id/perx_button"
    android:layout_marginTop="70dip"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/perx_card_btn" />

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

</LinearLayout>


public class PerxDetailedFragment extends SupportMapFragment {

  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
 GoogleMap map;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

     FragmentTransaction ft = getFragmentManager().beginTransaction();

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

    if (fmap == null) {
        fmap = SupportMapFragment.newInstance();
        ft.add(R.id.map, fmap);
    }

    ft.commit();

    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
            .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
            .position(KIEL)
            .title("Kiel")
            .snippet("Kiel is cool")
            .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);


     return inflater.inflate(R.layout.perx_detailed, null, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);       


    }


}
4

4 回答 4

6

您需要先膨胀布局。问题是您在 onCreateView 结束时执行此操作,因此当您调用 findFragmentById 时地图片段不存在。

此外,PerxDetailedFragment 应该扩展 Fragment,而不是 SupportMapFragment。

public class PerxDetailedFragment extends Fragment {
    // ...

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

        View v = inflater.inflate(R.layout.perx_detailed, null, false);

        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

        //...

        return v;
    }
于 2013-03-11T23:10:03.357 回答
0

删除所有 fmap 代码:

SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

if (fmap == null) {
    fmap = SupportMapFragment.newInstance();
    ft.add(R.id.map, fmap);
}

您已经将地图实例保存为“地图”变量,因此请使用该实例。

于 2013-03-09T20:58:12.467 回答
0

您需要扩展 SupportMapFragment

public class MapFragment extends SupportMapFragment{

GoogleMap mGoogleMap;
Context mContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mContext=getActivity();
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    initializeMap(rootView);
    return rootView;
}
void initializeMap(View rootView)
{
    if (mGoogleMap == null) {
        mGoogleMap=((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map4)).getMap();
        // Getting Map for the SupportMapFragment
        mGoogleMap.setMyLocationEnabled(true);
            if (mGoogleMap == null) {
            Toast.makeText(getActivity().getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        } else {
            setMapDefualtProp();

        }
    }else
        setMapDefualtProp();
}

private void setMapDefualtProp() {
    mGoogleMap.setMyLocationEnabled(true);
    mGoogleMap.setTrafficEnabled(true);
    mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
    mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
    mGoogleMap.getUiSettings().setCompassEnabled(true);
    mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
    mGoogleMap.getUiSettings().setZoomControlsEnabled(true);        
} 

}

于 2014-08-26T10:56:19.753 回答
0
gMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
于 2014-12-06T15:47:36.517 回答