我正在使用谷歌地图v2 API,当我要使用getMap方法获取地图时,它返回null

时间:2015-01-08 07:17:54

标签: android google-maps

这是片段类......

public class LocationFragment extends Fragment {
    GoogleMap googleMap=null;
    Location_Manager lm=new Location_Manager();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_location, container, false);
        googleMap=lm.init();
        return v;

    }
}

我调用Location_Manager类和使用init()方法....

public class Location_Manager extends FragmentActivity{
    GoogleMap googleMap=null;
    FragmentManager fm;
    SupportMapFragment smf;
    Handler handler=new Handler();

    public Location_Manager() {

        // TODO Auto-generated constructor stub
    }

    public GoogleMap init(){
        fm=getSupportFragmentManager();
        smf = (SupportMapFragment) fm.findFragmentById(R.id.google_map);
       new Handler().postDelayed(new Runnable(){
           @Override
        public void run() {
            // TODO Auto-generated method stub
         googleMap =smf.getMap();
         googleMap.addMarker(new MarkerOptions().position(new LatLng(37, 123)).title("sample"));

        }
       }, 500);



        return googleMap;

    }

}

googleMap = smf.getMap();有错误..日志告诉我googleMap为空...

我已经使用过handler和Supportmapfragment,但我无法解决这个问题。 我怎么解决??? 请帮助我;

1 个答案:

答案 0 :(得分:0)

尝试按照Android Google开发人员网站https://developers.google.com/maps/documentation/android/map

的使用入门进行操作

您还可以查看附加内容中的示例:

   package com.example.mapdemo;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * This shows how to create a simple activity with a map and a marker on the map.
 */
public class BasicMapDemoActivity extends FragmentActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basic_demo);

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     */
    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}