开发者社区> 问答> 正文

java.lang.ClassCastException: android.os.报错

"

onServiceConnected时异常如下:
崩溃线程

java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.ayst.healthkit.cq at com.ayst.healthkit.bp.onServiceConnected(MainActivity.java:492) at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1140) at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1157) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5292) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) at dalvik.system.NativeStart.main(Native Method)

Activity代码:

ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Common.debug(TAG, "onServiceDisconnected");
            mUsbService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Common.debug(TAG, "onServiceConnected");
            mUsbService = ((UsbTransferServer.UsbBinder)service).getService();
        }
    };

Service代码:

  @Override
    public IBinder onBind(Intent intent) {
        return new UsbBinder();
    }

    public class UsbBinder extends Binder {
        public UsbTransferServer getService(){
            return UsbTransferServer.this;
        }
    }

注:上面异常是偶现问题,测试50次出现过2次;

" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/55bd84d6877d4c19ab0a74b891fbcb16.png)

展开
收起
因为相信,所以看见。 2020-05-25 15:58:25 766 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载

相关镜像