如何在 activity 的 onResume() 部分访问服务对象的实例?

How to access the instance of service object in onResume() part of an activity?

有服务 activity。

服务 class 有一种方法

methodA().

现在我已经绑定了Activity中的服务,通过服务连接得到了服务的实例

问题是我无法访问 onResume() 方法中的服务实例

Public void onResume(){
forExample.  mService.methodA // is throwing null pointer exception
}

更新: 这就是我创建服务实例的方式

class A extends Activity{
public ServiceClass mService = null; // service objecct
void onCreate(){
       Intent ServiceIntent = new Intent(this,BleWrapper.class);    
       bindService(ServiceIntent,mServiceConnection,BIND_AUTO_CREATE);
       mLeService = BLEService.getInstance();
} 
Public void onResume(){
  mService.methodA // is throwing null pointer exception
}
}

我的服务连接是

public final ServiceConnection mServiceConnection= new ServiceConnection() {  
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mService = ((BleWrapper.LocalBinder) service).getService();
)

谁能帮帮我。

您可以在 Activity 的其他地方获取该服务的实例。结果可以给onResume()方法

当然会抛出 NullPointerException 因为你 根本没有初始化 服务。

onCreate()方法中初始化一个服务,然后就可以在onResume()

上使用mService.methodA()
class A extends Activity{
    public ServiceClass mService = null; // service objecct

    public void onCreate(){
        mService = new ServiceClass();
    }

    public void onResume(){
        mService.methodA();
    }
}

注意:无论何时创建任何对象,都必须在使用前对其进行初始化。