我正在創建一個Android應用程序,並且我已經在屏幕上啓動並運行了Google地圖。不過,我想在第一次加載地圖時捕捉到一點。即倫敦。Android應用程序 - 如何讓Google Maps API使用animateTo()?
我已經四處搜尋,嘗試了不同的方法,但是我無法讓它工作。
公共類MinuteMapActivity擴展MapActivity {
MapView mapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//the methods on the map are initialised here
SnapToMap();
}
MapController SnapToMap = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"}; //these values are from a website, how do I get the co-ordinates from google maps for i.e. London?
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint gp = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
SnapToMap.animateTo(gp);
SnapToMap.setZoom(15);
mapView.invalidate();
}
這是我寫的。難道我做錯了什麼?它不斷出現運行時錯誤並關閉應用程序。我是否需要向XML佈局添加一些內容?有人可以解釋什麼是mapView.invalidate()嗎?我此源代碼的部分從mobiforge.com
logcat的結果:
12-05 01:15:07.245: D/AndroidRuntime(18945): Shutting down VM
12-05 01:15:07.245: W/dalvikvm(18945): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
12-05 01:15:07.250: E/AndroidRuntime(18945): FATAL EXCEPTION: main
12-05 01:15:07.250: E/AndroidRuntime(18945): java.lang.RuntimeException: Unable to start activity ComponentInfo{application.project.minutemap/application.project.minutemap.MinuteMapActivity}: java.lang.NullPointerException
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.os.Looper.loop(Looper.java:130)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.app.ActivityThread.main(ActivityThread.java:3691)
12-05 01:15:07.250: E/AndroidRuntime(18945): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 01:15:07.250: E/AndroidRuntime(18945): at java.lang.reflect.Method.invoke(Method.java:507)
12-05 01:15:07.250: E/AndroidRuntime(18945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
12-05 01:15:07.250: E/AndroidRuntime(18945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
12-05 01:15:07.250: E/AndroidRuntime(18945): at dalvik.system.NativeStart.main(Native Method)
12-05 01:15:07.250: E/AndroidRuntime(18945): Caused by: java.lang.NullPointerException
12-05 01:15:07.250: E/AndroidRuntime(18945): at application.project.minutemap.MinuteMapActivity.SnapToMap(MinuteMapActivity.java:61)
12-05 01:15:07.250: E/AndroidRuntime(18945): at application.project.minutemap.MinuteMapActivity.onCreate(MinuteMapActivity.java:24)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-05 01:15:07.250: E/AndroidRuntime(18945): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-05 01:15:07.250: E/AndroidRuntime(18945): ... 11 more
的NullPointerException在61行:MapController snapToMap = mapView.getController();
我該如何擺脫這個?
你爲什麼把這個標記爲'iPhone'?我刪除了該標籤,因爲它不正確;只標記你的問題的主題,他們有關。 –
運行時錯誤會告訴你該怎麼做。在logcat中查看以獲取堆棧跟蹤並將其發佈到此處。 – Craigy
你是否使用'SnapToMap'作爲方法名和變量名? FYI中的慣例是使用小寫字母作爲變量和方法名稱中的第一個字符。此外,你的代碼格式很混亂,你能確保它符合你的要求嗎?此外,異常指向第61行的空指針,因此請自行發佈。 – Craigy