0

有谁知道为什么我总是在“myService.getMountPoint();”中得到空指针?

我想我做的几乎和网上的例子一样。

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();
    }
}

}

4

1 回答 1

0

当您尝试访问该服务时,该服务尚未绑定 - 因此在这种情况下引用为空。在调用 onServiceConnected 并设置对服务的引用后,您应该做您想做的事情

于 2012-05-25T09:05:15.090 回答