java.lang.ClassCastException:android.os.BinderProxy无法强制转换为LocalBinder

The*_*mad 10 android classcastexception android-service android-service-binding

我有一个Service我试图绑定到我的主要Activity,但我收到了

java.lang.ClassCastException:android.os.BinderProxy无法强制转换为com.walintukai.rubix.ConnectionService $ LocalBinder.

我已在我的清单中声明了该服务.为什么会这样?

宣言宣言

<service android:name=".ConnectionService" />
Run Code Online (Sandbox Code Playgroud)

服务(简化代码)

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();
}
Run Code Online (Sandbox Code Playgroud)

}

主要活动

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);
}
Run Code Online (Sandbox Code Playgroud)

}

堆栈跟踪

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)
Run Code Online (Sandbox Code Playgroud)

Byt*_*ger 11

这个问题已经有几年了,接受的答案对我不起作用.自从提出问题并回答问题以来,Android环境中的某些内容可能已经发生了变化

但我偶然发现了一个有效的简单解决方案.我在SO上看到了另一个试图启动2个服务的问题,他们遇到了类似的错误(每当我声明并运行两个服务时,获取java.lang.ClassCastException:android.os.BinderProxy).其中一个答案(@Coeffect)提到2个服务不一定在同一个进程中运行.

我认为可能(可能?)在我的情况下,活动和服务是在不同的进程中运行的.所以我将android:process元素添加到我的活动和服务器中,并修复了它.

这是一个AndroidManifest.xml示例.请注意,<activity><service>标签都包括android:process=":location":

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.byteslinger.locationservice">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:process=":location"
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:process=":location"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:process=":location"
            android:name=".LocationService"
            android:enabled="true" />
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

  • 有趣的。因为当您在“应用程序”上设置流程时,它会自动为其余组件(活动、服务、接收器...)设置流程。而且,在“应用程序”上设置进程通常与根本不设置它没有什么不同,因为如果省略“进程”标签,所有组件都在同一进程中运行。 (3认同)
  • 为什么要将“android:process”添加到 Activity 中? (2认同)

dco*_*cow 8

你翻了翻:

public void onServiceConnected(ComponentName name, IBinder service) {
    LocalBinder binder = (LocalBinder) service;
    service = (IBinder) binder.getService();
    ...
}
Run Code Online (Sandbox Code Playgroud)

应该:

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

  • 我刚刚弄清楚,但既然你同时发帖,我就给你积分。 (2认同)