2

我在片段中的以下代码执行时遇到 numm 指针异常。

 MapFragment mapFragment = MapFragment.newInstance();
 FragmentTransaction fragmentTransaction =  getFragmentManager().beginTransaction();
 fragmentTransaction.add(R.id.container1, mapFragment);
 fragmentTransaction.commit();
 mGoogleMap=mapFragment.getMap();
 mGoogleMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(22.03, 72.56)).title("hello world").snippet("Ahmedabad").icon(BitmapDescriptorFactory.fromResource(R.drawable.wifi)));

03-01 15:59:49.370:E/AndroidRuntime(12577):引起:java.lang.NullPointerException 03-01 15:59:49.370:E/AndroidRuntime(12577):在 com.tfick.fragment.PoiFragment.onCreateView (PoiFragment.java:64) 03-01 15:59:49.370: E/AndroidRuntime(12577): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829) 03-01 15:59:49.370: E/AndroidRuntime (12577): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 03-01 15:59:49.370: E/AndroidRuntime(12577): 在 android.app.BackStackRecord.run(BackStackRecord.java:635) 03-01 15:59:49.370: E/AndroidRuntime(12577): 在 android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)

4

3 回答 3

0

我已经解决了问题。这是解决方案Google Maps Android API v2 - 添加标记失败

于 2013-03-01T10:34:33.660 回答
0

看来,getMap()回报null。可能的解决方案:

  • 您是否检查GooglePlayServicesUtil.isGooglePlayServicesAvailable(context)过 Google Play 服务是否可用?
  • 地图可能尚未初始化,getMap()请在生命周期的后期尝试,例如onResume()
于 2013-03-01T10:49:08.817 回答
0

这是我发现的问题的解决方案。

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tfick.android.R;

public class PoiFragment extends MapFragment {
    /*object of google map*/
    public GoogleMap mGoogleMap;
    /* summy location*/
    private static final LatLng SYDNEY = new LatLng(23.03, 72.62);


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

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        if (getMap() != null) {
            Log.d(getClass().getSimpleName(), "Map ready for use!");

            mGoogleMap = getMap();

            /* cretaing custom camera position */

            CameraPosition mCameraPosition = new CameraPosition.Builder()
                    .target(SYDNEY).bearing(0).tilt(30).build();
            Log.i("gmap", mGoogleMap.toString());

            /* setting custom camera position */

            mGoogleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(mCameraPosition));

            /* adding custom marker on the map. */
            mGoogleMap
                    .addMarker(new MarkerOptions()
                            .draggable(true)
                            .position(new LatLng(22.03, 72.56))
                            .title("hello world")
                            .snippet("Ahmedabad")
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.wifi)));

            mGoogleMap
                    .addMarker(new MarkerOptions()
                            .draggable(true)
                            .position(new LatLng(23.03, 78.10))
                            .title("hello world")
                            .snippet("Baroda")
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.wifi)));

        }

    }

}
于 2013-03-02T04:33:21.067 回答