我有一个项目应该从位置管理器接收当前位置,然后更新向用户显示这些坐标的活动中的文本视图。我们希望位置管理器在后台作为服务运行。目前,即使我打开了活动,只要从服务中调用位置更新和更新 textViews 的方法,我们就会强制关闭。我也无法将方法更改为静态方法,因为静态与 textViews 不兼容。下面是服务、被调用的方法和堆栈跟踪。
位置更改时调用的服务中的方法
public void onLocationChanged(Location location) {
currentLocation = location;
SplashActivity.historyList.add(currentLocation);
Toast.makeText(getApplicationContext(), "Point added",
Toast.LENGTH_SHORT).show();
//postpones the update until NavigatorActivity starts
if (NavigatorStarted) {
new NavigatorActivity().doActionFine(currentLocation);
}
// these lines are controversial...
if (location.getAccuracy() > 1000 && location.hasAccuracy())
locationManager.removeUpdates(this);
}
活动中的方法,旨在通过准确的 GPS 信号或粗略信号更新具有新位置的文本视图。使用 do 操作方法从服务调用 LocationService 中的当前位置参数
public void updateLocationTextViewsFine(Location currentLocation) {
currentLatView = (TextView) findViewById(R.id.currentLat);
currentLonView = (TextView) findViewById(R.id.currentLon);
// update the textviews (labels)
String lat = currentLocation.getLatitude() + " (accurate)";
String lon = currentLocation.getLongitude() + " (accurate)";
currentLatView.setText(lat);
currentLonView.setText(lon);
}
public void updateLocationTextViewsCoarse(Location currentLocation) {
currentLatView = (TextView) findViewById(R.id.currentLat);
currentLonView = (TextView) findViewById(R.id.currentLon);
// update the textviews (labels)
String lat = currentLocation.getLatitude() + " (low accuracy)";
String lon = currentLocation.getLongitude() + " (low accuracy)";
currentLatView.setText(lat);
currentLonView.setText(lon);
}
public void doActionFine(Location currentLocation) {
updateLocationTextViewsFine(currentLocation);
}
public void doActionCoarse(Location currentLocation) {
updateLocationTextViewsCoarse(currentLocation);
}
收到新错误。
04-20 20:15:33.894: E/AndroidRuntime(4389): FATAL EXCEPTION: main
04-20 20:15:33.894: E/AndroidRuntime(4389): java.lang.NullPointerException
04-20 20:15:33.894: E/AndroidRuntime(4389): at android.app.Activity.findViewById(Activity.java:1637)
04-20 20:15:33.894: E/AndroidRuntime(4389): at com.example.UVAMapsProject.NavigatorActivity.updateLocationTextViewsFine(NavigatorActivity.java:341)
04-20 20:15:33.894: E/AndroidRuntime(4389): at com.example.UVAMapsProject.NavigatorActivity.doActionFine(NavigatorActivity.java:360)
04-20 20:15:33.894: E/AndroidRuntime(4389): at com.example.UVAMapsProject.LocationService$2.onLocationChanged(LocationService.java:116)
04-20 20:15:33.894: E/AndroidRuntime(4389): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
04-20 20:15:33.894: E/AndroidRuntime(4389): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
04-20 20:15:33.894: E/AndroidRuntime(4389): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
04-20 20:15:33.894: E/AndroidRuntime(4389): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 20:15:33.894: E/AndroidRuntime(4389): at android.os.Looper.loop(Looper.java:123)
04-20 20:15:33.894: E/AndroidRuntime(4389): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 20:15:33.894: E/AndroidRuntime(4389): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 20:15:33.894: E/AndroidRuntime(4389): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 20:15:33.894: E/AndroidRuntime(4389): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-20 20:15:33.894: E/AndroidRuntime(4389): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-20 20:15:33.894: E/AndroidRuntime(4389): at dalvik.system.NativeStart.main(Native Method)