我有一个应该从位置管理器接收当前位置的项目,然后更新显示这些坐标给用户的活动中的文字浏览。我们希望位置管理器在后台作为服务运行。目前,即使我打开了该活动,只要位置更新和更新textViews的方法从服务中调用,我们就会得到一个合力。我也无法将方法更改为静态方法,因为静态方法与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)
中显示的方案,并且您正在推迟,直到活动开始?你好吗?等待onCreate?在onStart?因为它看起来好像你的视图没有被初始化或者你的当前位置没有被初始化 – JRaymond 2012-04-20 23:37:06
我们在第一个活动开始的时候就开始了服务,但是推迟了更新文字浏览直到NavigatorActivity使用布尔值不要调用NavigatorActivity.update ...除非navigatorActivityStarted == true – rymillerr 2012-04-20 23:40:10
我们的意见和当前位置已经。当我运行应用程序时,我会收到一个表示当前位置已初始化并更新的祝酒。每次都在工作,并且在我已经在导航器活动中并且可以看到视图因此我们不会收到错误,因此他们必须初始化。 – rymillerr 2012-04-20 23:54:22