本文介绍了java.lang.ClassCastException:android.os.BinderProxy不能转换为LocalBinder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,我想结合我的主要活动,但是我收到一个

java.lang.ClassCastException:android.os.BinderProxy不能转换为com.walintukai.rubix.ConnectionService $ LocalBinder

我宣布我的清单服务。为什么会出现这种情况?

舱单申报

 <服务机器人:名字=。ConnectionService/>

服务(简体code)

 公共类ConnectionService延伸服务{静态最后弦乐TAG = ConnectionService.class.getName();私人BluetoothAdapter mBtAdapter = NULL;
公共BluetoothGatt mBluetoothGatt = NULL;
私人ConnectionServiceEventListener mIRedBearServiceEventListener;
HashMap的<弦乐,BluetoothDevice类> mDevices = NULL;
私人BluetoothGattCharacteristic txCharc = NULL;
私人最终的IBinder mBinder =新LocalBinder();@覆盖
公众的IBinder onBind(意向意图){
    返回mBinder;
}公共类LocalBinder扩展粘结剂{
    公共ConnectionService的getService(){
        返回ConnectionService.this;
    }
}@覆盖
公共无效的onCreate(){
    super.onCreate();    最后BluetoothManager bluetoothManager =(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
    mBtAdapter = bluetoothManager.getAdapter();    如果(mBtAdapter == NULL)
        返回;    如果(mDevices == NULL)
        mDevices =新的HashMap<弦乐,BluetoothDevice类>();
}@覆盖
公共无效的onDestroy(){
    如果(mBluetoothGatt == NULL)
        返回;    mBluetoothGatt.close();
    mBluetoothGatt = NULL;    super.onDestroy();
}

}

主要活动

 公共类MainActivity扩展ActionBarActivity {私人ConnectionService服务;ServiceConnection连接=新ServiceConnection(){    @覆盖
    公共无效onServiceDisconnected(组件名名){
        Log.i(ConnectionService,断开连接);
        服务= NULL;
    }    @覆盖
    公共无效onServiceConnected(组件名名称,服务的IBinder){
        LocalBinder粘结剂=(LocalBinder)服务;
        服务=(的IBinder)binder.getService();        如果(服务!= NULL){
            Log.i(RedBearService,连接);        }
    }
};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    如果(savedInstanceState == NULL){
        。getSupportFragmentManager()调用BeginTransaction()加(R.id.container,ViewPagerFragment.newInstance())提交()。
    }
}@覆盖
公共无效调用onStart(){
    super.onStart();
    startService();
}@覆盖
公共无效的onStop(){
    super.onStop();
    stopService();
}私人无效startService(){
    意向书的服务=新意图(这一点,ConnectionService.class);
    bindService(服务,连接Context.BIND_AUTO_CREATE);
}私人无效stopService(){
    unbindService(连接);
}

}

堆栈跟踪

  07-30 17:19:39.065:E / AndroidRuntime(20891):java.lang.ClassCastException:android.os.BinderProxy不能转换为com.walintukai.rubix.ConnectionService $ LocalBinder
07-30 17:19:39.065:E / AndroidRuntime(20891):在com.walintukai.rubix.MainActivity $ 1.onServiceConnected(MainActivity.java:58)
07-30 17:19:39.065:E / AndroidRuntime(20891):在android.app.LoadedApk $ ServiceDispatcher.doConnected(LoadedApk.java:1110)
07-30 17:19:39.065:E / AndroidRuntime(20891):在android.app.LoadedApk $ ServiceDispatcher $ RunConnection.run(LoadedApk.java:1127)
07-30 17:19:39.065:E / AndroidRuntime(20891):在android.os.Handler.handleCallback(Handler.java:733)
07-30 17:19:39.065:E / AndroidRuntime(20891):在android.os.Handler.dispatchMessage(Handler.java:95)
07-30 17:19:39.065:E / AndroidRuntime(20891):在android.os.Looper.loop(Looper.java:136)
07-30 17:19:39.065:E / AndroidRuntime(20891):在android.app.ActivityThread.main(ActivityThread.java:5050)
07-30 17:19:39.065:E / AndroidRuntime(20891):在java.lang.reflect.Method.invokeNative(本机方法)
07-30 17:19:39.065:E / AndroidRuntime(20891):在java.lang.reflect.Method.invoke(Method.java:515)
07-30 17:19:39.065:E / AndroidRuntime(20891):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)
07-30 17:19:39.065:E / AndroidRuntime(20891):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-30 17:19:39.065:E / AndroidRuntime(20891):在dalvik.system.NativeStart.main(本机方法)


解决方案

您已经得到它翻转:

 公共无效onServiceConnected(组件名名称,服务的IBinder){
    LocalBinder粘结剂=(LocalBinder)服务;
    服务=(的IBinder)binder.getService();
    ...
}

应该是:

 公共无效onServiceConnected(组件名名称,服务的IBinder){
    LocalBinder粘结剂=(LocalBinder)服务;
    MainActivity.this.service =(ConnectionService)binder.getService();
    ...
}

I have a Service that I am trying to bind to my main Activity, but I am receiving a

java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.walintukai.rubix.ConnectionService$LocalBinder.

I have declared the service in my manifest. Why is this happening?

Manifest Declaration

<service android:name=".ConnectionService" />

Service (simplified code)

public class ConnectionService extends Service {

static final String TAG = ConnectionService.class.getName();

private BluetoothAdapter mBtAdapter = null;
public BluetoothGatt mBluetoothGatt = null;
private ConnectionServiceEventListener mIRedBearServiceEventListener;
HashMap<String, BluetoothDevice> mDevices = null;
private BluetoothGattCharacteristic txCharc = null;
private final IBinder mBinder = new LocalBinder();

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

public class LocalBinder extends Binder {
    public ConnectionService getService() {
        return ConnectionService.this;
    }
}

@Override
public void onCreate() {
    super.onCreate();

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBtAdapter = bluetoothManager.getAdapter();

    if (mBtAdapter == null)
        return;

    if (mDevices == null)
        mDevices = new HashMap<String, BluetoothDevice>();
}

@Override
public void onDestroy() {
    if (mBluetoothGatt == null)
        return;

    mBluetoothGatt.close();
    mBluetoothGatt = null;

    super.onDestroy();
}

}

Main Activity

public class MainActivity extends ActionBarActivity {

private ConnectionService service;

ServiceConnection connection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.i("ConnectionService", "Disconnected");
        service = null;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        service = (IBinder) binder.getService();

        if (service != null) {
            Log.i("RedBearService", "Connected");

        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, ViewPagerFragment.newInstance()).commit();
    }
}

@Override
public void onStart() {
    super.onStart();
    startService();
}

@Override
public void onStop() {
    super.onStop();
    stopService();
}

private void startService() {
    Intent service = new Intent(this, ConnectionService.class);
    bindService(service, connection, Context.BIND_AUTO_CREATE);
}

private void stopService() {
    unbindService(connection);
}

}

Stacktrace

07-30 17:19:39.065: E/AndroidRuntime(20891): java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.walintukai.rubix.ConnectionService$LocalBinder
07-30 17:19:39.065: E/AndroidRuntime(20891):    at com.walintukai.rubix.MainActivity$1.onServiceConnected(MainActivity.java:58)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1110)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1127)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at android.os.Handler.handleCallback(Handler.java:733)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at android.os.Looper.loop(Looper.java:136)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at android.app.ActivityThread.main(ActivityThread.java:5050)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at java.lang.reflect.Method.invoke(Method.java:515)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-30 17:19:39.065: E/AndroidRuntime(20891):    at dalvik.system.NativeStart.main(Native Method)
解决方案

You've got it flipped:

public void onServiceConnected(ComponentName name, IBinder service) {
    LocalBinder binder = (LocalBinder) service;
    service = (IBinder) binder.getService();
    ...
}

should be:

public void onServiceConnected(ComponentName name, IBinder service) {
    LocalBinder binder = (LocalBinder) service;
    MainActivity.this.service = (ConnectionService) binder.getService();
    ...
}

这篇关于java.lang.ClassCastException:android.os.BinderProxy不能转换为LocalBinder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 20:44