簡體   English   中英

調用方法以從服務中更新活動中的TextViews

[英]Calling a method to update TextViews in an activity from a service

我有一個項目,該項目應該從位置管理器接收當前位置,然后在向用戶顯示這些坐標的活動中更新文本視圖。 我們希望位置管理器在后台作為服務運行。 當前,即使我打開了活動,只要從服務調用位置更新和更新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)

這可以通過使用活頁夾來完成:

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;
        }
    };
}

活動完成后,可以在取消綁定之前從服務中刪除偵聽器。

new NavigatorActivity().doActionFine(currentLocation);

剛剛看到了...。您正在創建活動的新實例並在其上調用方法。 這個新實例與創建用來打開鎖的活動不同,這是一個新活動,沒有調用生命周期方法。 我肯定會像其他答案中所示的那樣轉向Binder方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM