首页 > java.lang.ClassCastException: android.os.BinderProxy

java.lang.ClassCastException: android.os.BinderProxy

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


当你的Service和你的Activity不在同一个进程当中的时候,调用以上方法就会出错。请尝试使用的方式AIDL封装Binder。

【热门文章】
【热门文章】