2013-02-15 55 views
1

我正在使用谷歌地圖API for Android V2。當我加載地圖加載它完美 然而,當我試圖用這個代碼https://developers.google.com/maps/documentation/android/views#changing_a_maps_view我的應用程序崩潰時,我在谷歌地圖API使用moveCamera V2

public class MainActivity extends FragmentActivity { 

    private static final LatLng SYDNEY = new LatLng(-33.88,151.21); 
    private static final LatLng MOUNTAIN_VIEW = new LatLng(37.4, -122.1); 
    SupportMapFragment fragment = new SupportMapFragment(); 
    private GoogleMap map = fragment.getMap(); 
    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 


      getSupportFragmentManager().beginTransaction() 
        .add(android.R.id.content, fragment).commit(); 

      map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY,15)); 

      // Zoom in, animating the camera. 
      map.animateCamera(CameraUpdateFactory.zoomIn()); 

      // Zoom out to zoom level 10, animating with a duration of 2 seconds. 

       map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

      // Construct a CameraPosition focusing on Mountain View and animate the camera to that position. 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(MOUNTAIN_VIEW)  // Sets the center of the map to Mountain View 
       .zoom(17)     // Sets the zoom 
       .bearing(90)    // Sets the orientation of the camera to east 
       .tilt(30)     // Sets the tilt of the camera to 30 degrees 
       .build();     // Creates a CameraPosition from the builder 
      map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

    } 

的XML:當我運行代碼的應用程序崩潰

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/map" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
class="com.google.android.gms.maps.MapFragment"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/hello_world" /> 


    </RelativeLayout> 

它開始

不僅當我嘗試使用UiSettings類時也使用了這個。因此,我可能會缺少一些東西:)

任何幫助解決我的問題將不勝感激。

錯誤:

 02-15 17:44:41.701: E/AndroidRuntime(17078): FATAL EXCEPTION: main 
    02-15 17:44:41.701: E/AndroidRuntime(17078): java.lang.RuntimeException: Unable to s  tart activity ComponentInfo{com.example.anywhere_reminder/com.example.anywhere_reminder.MainActivity}: java.lang.NullPointerException: CameraUpdateFactory is not initialized 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at android.app.ActivityThread.access$600(ActivityThread.java:128) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at android.os.Handler.dispatchMessage(Handler.java:99) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at android.os.Looper.loop(Looper.java:137) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at android.app.ActivityThread.main(ActivityThread.java:4517) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at java.lang.reflect.Method.invokeNative(Native Method) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at java.lang.reflect.Method.invoke(Method.java:511) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:995) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at dalvik.system.NativeStart.main(Native Method) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at com.google.android.gms.internal.at.a(Unknown Source) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at com.google.android.gms.maps.CameraUpdateFactory.J(Unknown Source) 
    02-15 17:44:41.701: E/AndroidRuntime(17078):  at com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(Unknown Source) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at com.example.anywhere_reminder.MainActivity.onCreate(MainActivity.java:30) 
    02-15 17:44:41.701: E/AndroidRuntime(17078): at android.app.Activity.performCreate(Activity.java:4579) 
02-15 17:44:41.701: E/AndroidRuntime(17078): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) 
02-15 17:44:41.701: E/AndroidRuntime(17078): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 
02-15 17:44:41.701: E/AndroidRuntime(17078): ... 11 more 
+0

如果您不提供錯誤消息,則很難提供幫助。使用Logcat調試應用程序。 – joaoprudencio 2013-02-15 15:41:50

+0

@joaoprudencio編輯:) – proG 2013-02-15 15:50:36

回答

0

您只能使用CameraUpdateFactory如果GoogleMap的是可用的。

我會嘗試以下,把你的 私人GoogleMap map = fragment.getMap();

onCreate方法。如果地圖不爲空,那麼你可以調用CameraUpdateFactory。

map = fragment.getMap(); 
if(map != null){ 
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY,15)); 

      // Zoom in, animating the camera. 
      map.animateCamera(CameraUpdateFactory.zoomIn()); 

      // Zoom out to zoom level 10, animating with a duration of 2 seconds. 

       map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

      // Construct a CameraPosition focusing on Mountain View and animate the camera to that position. 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(MOUNTAIN_VIEW)  // Sets the center of the map to Mountain View 
       .zoom(17)     // Sets the zoom 
       .bearing(90)    // Sets the orientation of the camera to east 
       .tilt(30)     // Sets the tilt of the camera to 30 degrees 
       .build();     // Creates a CameraPosition from the builder 
      map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

} 
+1

仍然沒有工作:( – proG 2013-02-15 16:05:59

+0

你有相同的錯誤? – joaoprudencio 2013-02-15 16:08:55

+0

好吧!我錯過了manifest.xml中的標籤!現在它不克拉,但相機沒有設置在我指定的位置上代碼 – proG 2013-02-15 20:08:53

1

我認爲這些是你的代碼中的問題。

SupportMapFragment fragment = new SupportMapFragment(); 
private GoogleMap map = fragment.getMap(); 

對象片段爲空,您的GoogleMap地圖對象可能爲空。你應該這樣做。

SupportMapFragment sMapFragment; 
    GoogleMap map; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //get the MapFragment in your layout 
     fragment =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
     //get the GoogleMap object 
     map = fragment.getMap(); 

    } 

現在你已經準備好在地圖上做點什麼了。示例縮放到特定位置。

if(map!=null){ 
     LatLng SYDNEY = new LatLng(-33.88,151.21); 
     CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 16); 
     map.animateCamera(cameraUpdate); 
    } 

我希望這有助於。快樂編碼:)

相關問題