为什么在尝试使用Android Local Bound Service时出现空指针?

时间:2017-02-08 05:00:36

标签: android service

在服务类中:

//display: block;

在客户活动中:

```
elasticsearch_discovery_zen_ping_unicast_hosts={% for host in groups['tag_Function_logdb']  %}
{{ host }}:9300
{%- if not loop.last %},{% endif %}
{% endfor %}
```

在客户的onCreate()中:

class AeroBluetoothService extends Service { ...
private final IBinder asBleBinder = new LocalBinder();
public class LocalBinder extends Binder {
    AeroBluetoothService getService() {
        return AeroBluetoothService.this;
    } 
} 

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

问题在于,当我尝试从客户端调用Service方法时:

Intent bindAsBleIntent;

@Override
public void onServiceConnected( ComponentName className, IBinder service ) {
    AeroBluetoothService.LocalBinder asBleBinder = (AeroBluetoothService.LocalBinder) service;
    asBleServiceRef = asBleBinder.getService();
}

对服务实例的引用bindAsBleIntent = new Intent( getApplicationContext(), AeroBluetoothService.class ); bindService( bindAsBleIntent, /*ServiceConnection*/ this, Context.BIND_AUTO_CREATE ); asBleServiceRef.scanForAero(); 。就好像没有调用asBleServiceRef回调(或传递空参数)。

我从Android示例中非常仔细地复制了这段代码。我刚刚注意到该示例从null方法调用onServiceConnected(),而我从bindService()调用。这会有什么不同吗?有什么问题?

1 个答案:

答案 0 :(得分:0)

正如@avinash正确看到的那样,问题只是忽略了在Manifest中声明服务。