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

サービスにアクセスしようとすると、サービスはまだバインドされていません。したがって、この場合、参照は null です。onServiceConnected が呼び出され、サービスへの参照が設定された後に、やりたいことを行う必要があります

于 2012-05-25T09:05:15.090 に答える