绑定服务时ClassCastException

时间:2014-10-11 16:55:17

标签: android android-service classcastexception

我正在关注Bound服务的教程:http://developer.android.com/guide/components/bound-services.html并在我的代码中使用它。但是,当我使用教程中的代码时,我在此行收到错误:service = binder.getService();无法从localService转换为IBinder。因此,作为解决方案,我将其投射到IBinder。但是当我开始运行应用程序时,我得到了一个classCastException。

这是MainActivity的代码:

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        service = (IBinder) binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};


@Override
protected void onStart() {
    super.onStart();
    // Bind to LocalService
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    // Unbind from the service
    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
}

我服务中的代码如下所示:

public class LocalService extends Service implements LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener, SensorEventListener {


/**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

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

我的清单文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.geotodo"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- Required for accessing our current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="KEY"/>

    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

    <service android:enabled="true" android:name=".LocalService" />
</application>

请帮助别人。

1 个答案:

答案 0 :(得分:1)

使用:

service = (LocalService) binder.getService();

...而不是:

service = (IBinder) binder.getService(); 

您正在调用Service对象,并尝试将其转换为投放到IBinder的{​​{1}}对象。