0

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

2 回答 2

2

这可以使用活页夹来完成:

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

然后在活动中按照上面文章中的示例进行操作。当您连接到服务时,询问当前位置(保存在服务或首选项中)。然后你给服务一个监听器,它会被调用。

这里有一些示例代码。缺少侦听器和 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;
        }
    };
}

活动完成后,在解除绑定之前从服务中移除侦听器。

于 2012-04-21T00:00:34.727 回答
1
new NavigatorActivity().doActionFine(currentLocation);

刚刚看到这个......你正在创建一个新的活动实例并调用它的方法。这个新实例与为打开锁而创建的活动不同,这是一个没有调用生命周期方法的新活动。我肯定会像另一个答案中显示的那样转向 Binder 方案

于 2012-04-21T00:36:09.263 回答