android aidl空指针
有人知道为什么我总是在“myService.getMountPoint();”中得到空指针吗 我想我做的和互联网上的例子差不多android aidl空指针,android,Android,有人知道为什么我总是在“myService.getMountPoint();”中得到空指针吗 我想我做的和互联网上的例子差不多 public class mainActivity extends Activity { /** Called when the activity is first created. */ private IMountService myService = null; private ServiceConnection conn = new ServiceConne
public class mainActivity extends Activity {
/** Called when the activity is first created. */
private IMountService myService = null;
private ServiceConnection conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myService = IMountService.Stub.asInterface(service);
}
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bindService(new Intent(mainActivity.this, DeviceStatusService.class), conn,
Context.BIND_AUTO_CREATE);
startService(new Intent(mainActivity.this, DeviceStatusService.class));
try {
myService.getMountPoint();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}当您尝试访问服务时,该服务尚未绑定-因此在本例中引用为空。调用onServiceConnected并设置对服务的引用后,您应该执行您想要执行的操作
IMountService myService=null代码>这就是原因。