IBinder Android-ClassCastException:Android.os.Handler&;MessgerImpl
在进程中使用IBinder for android时,我遇到了ClassCastException。遵循大多数在线指南,在我的服务中,我有IBinder Android-ClassCastException:Android.os.Handler&;MessgerImpl,android,android-service,android-service-binding,Android,Android Service,Android Service Binding,在进程中使用IBinder for android时,我遇到了ClassCastException。遵循大多数在线指南,在我的服务中,我有 public class TestBinder extends Binder { } 在我的当事人身上 private ServiceConnection testServiceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName c
public class TestBinder extends Binder {
}
在我的当事人身上
private ServiceConnection testServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
TestBinder mIBeaconBinder = (TestBinder)service;
.....
}
}
TestBinder mIBeaconBinder = (TestBinder)service; this throws the exception.
{
编辑
将该行改为-
既然您扩展了活页夹对象,您也实现了它的界面,即IBinder,您可以阅读更多内容,以防这不起作用(并且您仍然有一个例外),请看一看(在添加活页夹时),然后我为您标记了相关行。正确的类型是
testService mService = ((testService.TestBinder)service).getService();
是否要检索服务对象?是的,在TestBinder内部有一个getService方法,我希望通过调用此方法在客户端获取此实例。仔细查看,我没有发现我的实现中有任何差异…发布您的实现公共类testService扩展服务{public class TestBinder扩展Binder{public testService getService(){//返回LocalService的这个实例,这样客户端就可以调用public//方法Return testService.this;}}客户端代码与我上面发布的代码基本相同。谢谢。我的案例中棘手的部分是,在服务内部,我有另一个messenger,它是用处理程序创建的。因此,对于onBind(),我必须使用messenger.getBinder()。在客户端,使用bleService=((bleService.LocalBinder)service)。getService();仍然引发强制转换异常。我的服务端代码看起来非常类似。如果我更改onBind以返回ibinder实例而不是messenger.getBinder(),我将遇到与感谢中报告的问题相同的问题。我的案例中棘手的部分是,在服务内部,我有另一个messenger,它是用处理程序创建的。因此,对于我的onBind(),我必须使用messenger.getBinder()。在客户端,使用bleService=((bleService.LocalBinder)service)。getService();仍然引发强制转换异常。我的服务端代码看起来非常类似于如果我更改onBind以返回ibinder实例而不是messenger.getBinder(),我将遇到中报告的相同问题
IBinder mIBeaconBinder = (TestBinder)service;
testService mService = ((testService.TestBinder)service).getService();