2012-04-20 133 views
0

我有一个应该从位置管理器接收当前位置的项目,然后更新显示这些坐标给用户的活动中的文字浏览。我们希望位置管理器在后台作为服务运行。目前,即使我打开了该活动,只要位置更新和更新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) 
+0

中显示的方案,并且您正在推迟,直到活动开始?你好吗?等待onCreate?在onStart?因为它看起来好像你的视图没有被初始化或者你的当前位置没有被初始化 – JRaymond 2012-04-20 23:37:06

+0

我们在第一个活动开始的时候就开始了服务,但是推迟了更新文字浏览直到NavigatorActivity使用布尔值不要调用NavigatorActivity.update ...除非navigatorActivityStarted == true – rymillerr 2012-04-20 23:40:10

+0

我们的意见和当前位置已经。当我运行应用程序时,我会收到一个表示当前位置已初始化并更新的祝酒。每次都在工作,并且在我已经在导航器活动中并且可以看到视图因此我们不会收到错误,因此他们必须初始化。 – rymillerr 2012-04-20 23:54:22

回答

2

这可以使用粘合剂来完成:

http://developer.android.com/guide/topics/fundamentals/bound-services.html

然后在活动中做到这一点在上述文章中的例子。连接到服务时,请询问当前位置(保存在“服务”或“首选项”中)。然后你给服务的听众,这将被称为。

这里有一些示例代码。缺少Listener和LocalService类的实现。但只是为了展示我的意思。

public class BindingActivity extends Activity { 
    LocalService mService; 
    boolean mBound = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     // Bind to LocalService 
     Intent intent = new Intent(this, LocalService.class); 
     bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     // Unbind from the service 
     if (mBound) { 
      mService.removeListener(this); 
      unbindService(mConnection); 
      mBound = false; 
     } 
    } 

    private ServiceConnection mConnection = new ServiceConnection() { 

     @Override 
     public void onServiceConnected(ComponentName className, 
       IBinder service) { 
      // We've bound to LocalService, cast the IBinder and get LocalService instance 
      LocalBinder binder = (LocalBinder) service; 
      mService = binder.getService(); 
      mBound = true; 

      handleLocation(mService.getCurrentLocation()); 
      mService.addListener(BindingActivity.this); 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName arg0) { 
      mBound = false; 
     } 
    }; 
} 

活动结束后,您可以在解除绑定之前从服务中删除侦听器。

+1

同意这是正确的方法 – JRaymond 2012-04-21 00:28:21

+0

我不知道如何在绑定后在活动中调用updateLocationTextViews()方法。此外,接收错误,说onServiceConnected和onServiceDisconnected不覆盖 – rymillerr 2012-04-21 00:52:55

+0

LocalBinder来自哪里? – aggregate1166877 2013-04-04 10:17:42

1
new NavigatorActivity().doActionFine(currentLocation); 

刚才看到这个....你正在创建一个活动的新实例并调用它的方法。这个新实例不是创建来打开你的锁的活动,这是一个没有生命周期方法的新活动。我肯定会转移到活页夹方案,如其他答案

+0

基于建议使用上述方法。认为这似乎是可疑的。努力实现绑定。谢谢! – rymillerr 2012-04-21 00:43:50