gpt4 book ai didi

java - 当我在 google maps api V2 上使用 moveCamera 时我的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 03:46:34 25 4
gpt4 key购买 nike

我正在使用适用于 Android 的 google maps api V2。当我加载 map 时它加载完美但是,当我尝试使用来自 https://developers.google.com/maps/documentation/android/views#changing_a_maps_view 的代码时

 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

最佳答案

我认为这些是您的代码中的问题。

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

对象 fragment 为空,您的 GoogleMap map 对象可能为空。你应该这样做。

    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();

}

现在您已准备好在 map 上做一些事情。缩放到特定位置的示例。

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

希望对您有所帮助。快乐编码:)

关于java - 当我在 google maps api V2 上使用 moveCamera 时我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14897705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com