好的,所以我有一张简单的地图,在你移动时跟随你的位置。
我试图让它只有在点击一个切换按钮时才会这样做。
从长远来看,我希望制作一款能够跟踪旅程并将其存储在数据库中的应用程序。因此,当我按下开始时,我将把当前位置绘制到我当前的新位置,然后当我按下停止时,它将保存行程。
然而,当我添加一个onClick监听器时,我的应用程序只是在启动时崩溃。有什么想法吗?
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
if(mMap != null) {
//Enable GPS
mMap.setMyLocationEnabled(true);
Location previousLocation = mMap.getMyLocation();
final LatLng previousPos = new LatLng(previousLocation.getLatitude(), previousLocation.getLongitude());
final ToggleButton button = (ToggleButton) findViewById(R.id.startStop);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
if(button.isChecked()) {
//Set the map to current location
mMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
public void onMyLocationChange(Location location) {
LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
//Zoom parameter is set to 17
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(position, 17);
//Use map.animateCamera(update) if you want moving effect
mMap.moveCamera(update);
}
});
}
else {
}
}
});
}
}
}
这是logcat错误
11-09 19:06:38.143: E/AndroidRuntime(12275): FATAL EXCEPTION: main
11-09 19:06:38.143: E/AndroidRuntime(12275): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.hellomap/com.example.hellomap.MainActivity}: java.lang.NullPointerException
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.ActivityThread.access$700(ActivityThread.java:140)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.os.Looper.loop(Looper.java:137)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.ActivityThread.main(ActivityThread.java:4921)
11-09 19:06:38.143: E/AndroidRuntime(12275): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 19:06:38.143: E/AndroidRuntime(12275): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 19:06:38.143: E/AndroidRuntime(12275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
11-09 19:06:38.143: E/AndroidRuntime(12275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
11-09 19:06:38.143: E/AndroidRuntime(12275): at dalvik.system.NativeStart.main(Native Method)
11-09 19:06:38.143: E/AndroidRuntime(12275): Caused by: java.lang.NullPointerException
11-09 19:06:38.143: E/AndroidRuntime(12275): at com.example.hellomap.MainActivity.setUpMapIfNeeded(MainActivity.java:50)
11-09 19:06:38.143: E/AndroidRuntime(12275): at com.example.hellomap.MainActivity.onCreate(MainActivity.java:29)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.Activity.performCreate(Activity.java:5206)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-09 19:06:38.143: E/AndroidRuntime(12275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
答案 0 :(得分:0)
取出两行代码49和50.它现在有效! :)
// Location previousLocation = mMap.getMyLocation();
//final LatLng previousPos = new LatLng(previousLocation.getLatitude(), previousLocation.getLongitude());