2013-02-24 1 views
0

작업 표시 줄 탭에서 Maps v2를 구현하려고합니다. 지도 탭은 MapFragment에서 상속 된 단편입니다. 지도 탭을 클릭하면 앱이 강제 종료됩니다. getMap()이 호출 된 onStart 메소드 내에서 널 포인터 예외를 제공합니다. 다음은 코드입니다. 제 잘못이 어디인지 말해주세요.MapFragment의 onStart()에서 getMap()을 호출하면 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에서 null 맵을 가져 오는 것은 Google 서비스 API가 아직 시작되지 않았 음을 의미합니다.

종종 권한이 누락되었거나 잘못된 Google API 키가 있거나 호스트 기기에서 Google Play 서비스가 활성화되지 않아서 발생합니다.

GooglePlayServicesUtil.isGooglePlayServicesAvailable(anyActivity) 

어떤을 반환합니다

당신은

및지도 활동을 시작하기 전에, 당신의 가치에주의를 기울여야한다 당신 AndroidManifest를뿐만 아니라 maplayout.xml을 게시하여 더 많은 정보를 얻을 수 Google Play 서비스 상태에 대한 표시

+0

재생 서비스가 설치되어 있는지 확인해야하는 번거 로움 때문에 새 휴대 전화에이 기능이 설치되어 있지 않아 모든 앱에서이 문제를 처리해야합니다. – deepwinter

관련 문제