服务回调抛出:未捕获的远程异常!(尚不支持跨进程的异常.) [英] Service callback throws: Uncaught remote exception! (Exceptions are not yet supported across processes.)

查看:49
本文介绍了服务回调抛出:未捕获的远程异常!(尚不支持跨进程的异常.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的回调出现了一些问题.这是我的代码:

I'm having some issues with my callbacks. Here is my code:

活动:

private ICallback callback = new ICallback.Stub() {
    @Override
    public void fire() throws RemoteException {
        mTextView.setText("fired");
    }
};

//then in onCreate i add:
mManger.registerCallback(callback);

ICallback (AIDL)

ICallback (AIDL)

interface ICallback {
    void fire();
}

经理:

public void registerCallback(ICallback callback) {
    try {
        mService.registerCallback(callback);
    } catch (RemoteException e) {
        Log.e(TAG, "Service is dead");
    }
}

private void notifyCallbacks() {
    try {
        mService.notifyCallbacks();
    } catch (RemoteException e) {
        Log.e(TAG, "Service is dead");
    }
}

服务:

public void registerCallback(ICallback callback) {
    if (callback != null) {
         mCallbacks.register(callback);
    }
}

public void notifyCallbacks() {
    final int N = mCallbacks.beginBroadcast();

    for (int i=0;i<N;i++) {
        try {
            mCallbacks.getBroadcastItem(i).fire();
        } catch (RemoteException e) {
        }
    }
    mCallbacks.finishBroadcast();
}

我的回调得到通知,但我在尝试设置 textview 文本时遇到了这个问题:

My callbacks get notified but I run into this when it try to set the textview text:

E/JavaBinder:* 未捕获的远程异常!(例外尚未跨进程支持.)android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图.

E/JavaBinder﹕ * Uncaught remote exception! (Exceptions are not yet supported across processes.) android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

推荐答案

如错误消息所述,您尝试从错误的线程更新视图.由于 fire() 方法是从在另一个线程中运行的远程服务调用的,因此您需要确保更新 UI 的代码在 UI 线程中运行.为此,请尝试以下操作:

Like the error message says, you try to update a View from the wrong Thread. As the fire() method is called from the remote Service that runs in another Thread, you need to make sure, that the code that updates the UI runs in the UI Thread. To achieve this, try the following:

public void fire() throws RemoteException {

    //the activity provides this method to be able to run code in the UI Thread
    runOnUiThread(new Runnable(){

        @Override
        public void run(){
            mTextView.setText("fired");
        }
    })
}

这篇关于服务回调抛出:未捕获的远程异常!(尚不支持跨进程的异常.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆