5

我有一个扩展 SupportMapFragment 的片段。

public class PlaceMapsFragment extends SupportMapFragment {
    private GoogleMap mMap;
    private LatLng mPosFija;

    public PlaceMapsFragment() {
        super();
    }

    public static PlaceMapsFragment newInstance(LatLng posicion) {
        PlaceMapsFragment frag = new PlaceMapsFragment();
        frag.mPosFija = posicion;
        return frag;
    }

    @Override
    public GoogleMap getMap() {
        // TODO Auto-generated method stub
        return super.getMap();
    }

    @Override
    public void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        //initMap();
        return view;
    }

    private void initMap() {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija, 16));
        getMap().addMarker(
                new MarkerOptions().position(mPosFija)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.marker)));
    }

}

现在我想引用自动生成的 MapView,以便我可以将我的功能添加到地图中。

当我尝试initMap();得到NullPointerException

错误:

12-10 02:54:20.171: E/AndroidRuntime(4339): FATAL EXCEPTION: main
12-10 02:54:20.171: E/AndroidRuntime(4339): java.lang.NullPointerException
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.ar.b.a(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.y.h.a(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.y.au.a(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.y.ae.moveCamera(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:83)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Binder.transact(Binder.java:297)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.m7.nomad.PlaceMapsFragment.initMap(PlaceMapsFragment.java:55)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.m7.nomad.PlaceMapsFragment.onCreateView(PlaceMapsFragment.java:46)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.m7.nomad.PlaceActivity$TabManager.onTabChanged(PlaceActivity.java:153)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:379)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabHost.setCurrentTab(TabHost.java:364)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.view.View.performClick(View.java:3591)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.view.View$PerformClick.run(View.java:14263)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Handler.handleCallback(Handler.java:605)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Looper.loop(Looper.java:137)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.app.ActivityThread.main(ActivityThread.java:4507)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at java.lang.reflect.Method.invoke(Method.java:511)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

8

这是预期的行为。

getMap()只有在 Fragment 通过后才能调用onCreateView(),否则它会给你一个null. null如果 Google Play 服务不可用,您也可以收到。

换句话说,调用initMap().onResume()

你可以在这里查看更多。

希望能帮助到你!

于 2012-12-09T23:30:12.530 回答
6

我终于解决了,您不必重写getMap(),也不需要 mMap 变量,GoogleMap 包含在类中。最后你需要在 onResume 中调用 initMap():

public class PlaceMapsFragment extends SupportMapFragment {
    private LatLng mPosFija = new LatLng(37.878901,-4.779396);

    public PlaceMapsFragment() {
        super();
    }

    public static PlaceMapsFragment newInstance(LatLng posicion) {
        PlaceMapsFragment frag = new PlaceMapsFragment();
        frag.mPosFija = posicion;
        return frag;
    }

   @Override
   public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    initMap();
   }



    @Override
    public void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        //initMap();
        return view;
    }

    private void initMap() {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija, 16));
        getMap().addMarker(
                new MarkerOptions().position(mPosFija)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.marker)));
    }

}

希望它可以帮助其他开始使用 google maps api v2 的人(比如我:D)。

于 2013-01-24T09:17:34.443 回答
0

问题出在 onCreateView 中。你忘了调用 super.onCreateView()。检查这个

于 2013-09-27T20:57:41.080 回答