2013-04-14 2 views
16

저는 건물에있는 액티비티에 작은 MapFragment를 넣으려고하지만, 모든 작업을 수행하는 데 문제가 있습니다. Map API와 Play 서비스가 모두 제대로 설치되었다는 것을 알고 있습니다. 테스트 자습서를 작성하고 모든 것이 잘 작동했기 때문입니다.SupportMapFragment - Fragment에서 MapFragment로 캐스팅 할 수 없습니다.

문서 here에 따라 다음 문제가 발생합니다. 내 setUpMapIfNeeded 메서드에서 getFragmentManager() 또는 getSupportFragmentManager()을 사용할 수 있습니다. getFragmentManager()을 사용할 때 Eclipse는 멋지지만 실행하면 NoSuchMethodError이라는 메서드가 정의되지 않았 음을 알게됩니다. 내가 getSupportFragmentManager()을 선택할 때, Eclipse는 그것을 좋아하지 않고 "Fragment에서 MapFragment로 캐스트 할 수 없다"라는 오류를 내게줍니다. 그럼 거래는 뭐니? 어떤 제안?

private void setUpMapIfNeeded() { 
    //Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     if (mMap != null) { 
      //do things to the map 
      mMap.addMarker(new MarkerOptions().position(LOCATION).title(EXTRA_URL)); 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LOCATION,15)); 
      mMap.getUiSettings().setZoomControlsEnabled(false); 
     } 
    } 
} 

내가 제공 할 수있는 다른 코드가 있으면 알려 주시면 신속하게 게시 해 드리겠습니다.

+0

R.id.map에 class 속성이 있습니까? 'class = "com.google.android.gms.maps.MapFragment"' –

답변

77

getFragmentManager() 또는 getSupportFragmentManager()를 사용할 수 있습니다.

여기에는 논란의 여지가 없어야합니다. getSupportFragmentManager()을 사용할 수있는 경우 Android 지원 패키지의 조각 백 포트를 사용하고 있으며이 방법을 사용해야합니다.

Eclipse에서 getSupportFragmentManager()를 선택하면 Eclipse에서 "조각에서 MapFragment로 캐스팅 할 수 없습니다"라는 오류 메시지가 표시됩니다.

MapFragment을 사용해서는 안되기 때문입니다. Android 지원 패키지의 조각 백 포트를 사용 중이므로 SupportMapFragment을 사용해야합니다.

+0

나는 정말 어리 석다. 나는 확실히 알고 있었지만 분명히 SupportMapFragment로 변경하지 않았습니다. SupportMapFragment가 아닌 MapFragment를 가져오고 있습니다. 감사! 내 문제를 해결했다. – user2163853

+0

@ user2163853 또한 어리석은 실수로 여기에 빠졌습니다! –

+2

때때로 어리석은 실수는 다른 사람 (아마 초보자)이 개념을 파악하는 데 도움이되므로 너무 어리 석을 수 없습니다. – marienke

0

내가 레벨 11에서 작업했기 때문에 이것은해야만하는 작업입니다.

import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.SupportMapFragment; 
    import android.support.v4.app.FragmentActivity; 

    public class MapaActivity extends FragmentActivity { 

    private GoogleMap map; 

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

     map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapa)).getMap(); 
    } 
} 
0

헤이 간단하게 폴더를 단지

수입 android.support.v4.app.FragmentActivity을 가져 당신의 조각 페이지에서 다음 를 구축하는 추가하려면 libs가에 V4 항아리를 추가;

당신은 할 수는 FragmentActivity

공용 클래스 MainActivity가 FragmentActivity가 LocationListener {

}

구현하고 그것도에서 getSupportFragmentManager을()에 액세스 확장 확장 할 수 있습니다;

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager(). findFragmentById (R.id.map);

관련 문제