java.lang.ClassCastException: android.os.BinderProxy
高洛峰
高洛峰 2017-04-17 13:00:43
0
1
631

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
巴扎黑

When your Service and your Activity are not in the same process, calling the above method will cause an error. Please try to use AIDL to encapsulate Binder.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template