2013-02-24 30 views
0

我想在操作欄選項卡中實現Maps v2。地圖選項卡是從MapFragment繼承的片段。 點擊地圖標籤後,應用程序強制關閉。它在調用getMap()的onStart方法內部提供了空指針異常。 這是代碼。請告訴我錯在哪裏。在MapFragment中調用getMap()在onStart()給出nullpointerexception

public class MapActivity extends MapFragment implements LocationListener { 
int mNum; 
GoogleMap googleMap; 


public static MapActivity newInstance() { 
    MapActivity f = new MapActivity(); 

    // Supply num input as an argument. 
    Bundle args = new Bundle(); 
    //args.putInt("num", num); 
    //f.setArguments(args); 

    return f; 
} 

/** 
* When creating, retrieve this instance's number from its arguments. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.maplayout, container, false); 

    return v; 
} 


/** 
* The Fragment's UI is just a simple text view showing its 
* instance number. 
*/ 
public void onStart(){ 
    super.onStart(); 

    googleMap=getMap(); 

    // Enabling MyLocation Layer of Google Map 
      googleMap.setMyLocationEnabled(true);    


      getActivity(); 
      // Getting LocationManager object from System Service LOCATION_SERVICE 
      LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 

      // Creating a criteria object to retrieve provider 
      Criteria criteria = new Criteria(); 

      // Getting the name of the best provider 
      String provider = locationManager.getBestProvider(criteria, true); 

      // Getting Current Location 
      Location location = locationManager.getLastKnownLocation(provider); 

      if(location!=null){ 
        onLocationChanged(location); 
      } 

      locationManager.requestLocationUpdates(provider, 20000, 0, this); 


     // Setting a click event handler for the map 
      googleMap.setOnMapClickListener(new OnMapClickListener() { 

       @Override 
       public void onMapClick(LatLng latLng) { 

        // Creating a marker 
        MarkerOptions markerOptions = new MarkerOptions(); 

        // Setting the position for the marker 
        markerOptions.position(latLng); 

        // Setting the title for the marker. 
        // This will be displayed on taping the marker 
        markerOptions.title(latLng.latitude + " : " + latLng.longitude); 

        // Clears the previously touched position 
        //googleMap.clear(); 

        // Animating to the touched position 
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 

        // Placing a marker on the touched position 
        googleMap.addMarker(markerOptions); 
       } 
      }); 


     } 


     @Override 
     public void onLocationChanged(Location location) { 

      TextView tvLocation = (TextView) getActivity().findViewById(R.id.tv_location); 

      // Getting latitude of the current location 
      double latitude = location.getLatitude(); 

      // Getting longitude of the current location 
      double longitude = location.getLongitude();  

      // Creating a LatLng object for the current location 
      LatLng latLng = new LatLng(latitude, longitude); 

      // Showing the current location in Google Map 
      googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

      // Zoom in the Google Map 
      googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 

      // Setting latitude and longitude in the TextView tv_location 
      tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude);  

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub  
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub  
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub  
     }  

}

編輯:我認爲這個代碼不應該在OnStart方法寫,因爲沒有再顯示在地圖。如何知道地圖已被加載,以獲取其對象?

回答

1

在OnStart中獲取空映射表示Google Services API尚未啓動。

這通常是由於在主機設備上未激活權限或無效的Google API密鑰或Google Play服務。

GooglePlayServicesUtil.isGooglePlayServicesAvailable(anyActivity) 

將返回:

您可以通過張貼你AndroidManifest以及你maplayout.xml

並推出自己的地圖活動之前,你應該照顧的價值得到更多的信息關於Google Play服務狀態的指示

+0

需要檢查是否安裝了播放服務的良好通話,所有應用都需要處理此問題,因爲新手機不會安裝該服務。 – deepwinter 2013-04-23 20:06:18

相關問題