0

尝试在我的应用中实现应用内购买 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 方法的一部分。任何指针?

4

1 回答 1

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();
    }
于 2013-09-08T15:18:09.357 回答