尝试在我的应用中实现应用内购买v3时,我看到了null异常。以下是我的代码:
mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
MainActivity.this.bindService(new Intent(
"com.android.vending.billing.InAppBillingService.BIND"),
mServiceConn, Context.BIND_AUTO_CREATE);
try {
System.out.println("mService " + mService);
Bundle buyIntentBundle = mService
.getBuyIntent(
3,
getPackageName(),
"android.test.purchases",
"inapp",
"aaa");
PendingIntent pendingIntent = buyIntentBundle
.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(), 1001,
new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SendIntentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我看到的例外是服务为空。我放在代码中的sysout是打印null。以上所有代码都是我活动的onCreate方法的一部分。有什么指针吗?
答案 0 :(得分:0)
任何人都在努力解决这个问题,只需将以下代码移到onServiceConnected方法即可。我们不应该尝试访问服务,直到它受到约束。
try {
System.out.println("mService " + mService);
Bundle buyIntentBundle = mService
.getBuyIntent(
3,
getPackageName(),
"android.test.purchases",
"inapp",
"aaa");
PendingIntent pendingIntent = buyIntentBundle
.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(), 1001,
new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SendIntentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}