我在 MyFragment.class 中的bind()
呼叫onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
而onResume()
我正在尝试访问 mSerive 但是它在onServiceConnected(ComponentName name, IBinder service)
之后显示 NULL POINTER EXCEPTION ,onResume()
来电。为什么在我调用bind()
时没有调用onServiceConnected可以有人给出关于它的建议吗?
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
try {
mService.unregisterCallback(mCallback.this);
mService = null;
} catch (RemoteException e) {
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
try {
mService = IService.Stub.asInterface(service);
mService.registerCallback(mCallback.this);
} catch (RemoteException e) {
}
}
};
public void bind() {
if (!mServiceConnected) {
mServiceConnected = true;
Intent connectionIntent = new Intent(IService.class.getName());
mActivity.bindService(connectionIntent, mServiceConnection,
Context.BIND_AUTO_CREATE);
}
}
答案 0 :(得分:0)
试试这个
// inapp
Intent serviceIntent = new Intent(
"com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService = bindService(serviceIntent, mServiceConn,
Context.BIND_AUTO_CREATE);