7

我有以下方法(第一次效果很好):

public void TipUs(){ 
    String sku="tip_us";

    try {
    Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
                                                   sku, "inapp", "TIP_US");
    PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
    startIntentSenderForResult(pendingIntent.getIntentSender(), 
                               1001, new Intent(), 
                               Integer.valueOf(0), Integer.valueOf(0),
                               Integer.valueOf(0));
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (SendIntentException e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode == 1001) 
        {       
            int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
            String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
            String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

            if (resultCode == RESULT_OK) {
                try {
                    JSONObject jo = new JSONObject(purchaseData);
                    String sku = jo.getString("productId");
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
}

如果我再次尝试使用相同的方法(根据 Google Play 中的设置应该可以),我会收到以下错误:

java.lang.NullPointerException
at com.appiclife.tipcal.Tip_Calculator.TipUs(Tip_Calculator.java:521)
at com.appiclife.tipcal.Tip_Calculator.onClick(Tip_Calculator.java:350)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

有没有人有这个错误,因为我完全使用了谷歌的演示,我应该改变什么?

编辑:看起来 mService 是空的(虽然奇怪的是,它只工作了一次,所以如果你不止一次购买同一个项目,这看起来几乎是个问题)。我有以下内容:

这是我的代码:

OnCreate()

Intent("com.android.vending.billing.InAppBillingService.BIND"),
                    mServiceConn, Context.BIND_AUTO_CREATE);

和:

IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() {

     public void onServiceConnected(ComponentName name, 
              IBinder service) {

                System.out.println("Test!");
               mService = IInAppBillingService.Stub.asInterface(service);
     }

     public void onServiceDisconnected(ComponentName name) {
       mService = null;
     }


};

onServiceConnected 永远不会被调用。我在手册中没有看到这个:http: //developer.android.com/google/play/billing/billing_integrate.html

但我尝试将以下服务添加到清单中,但没有结果(我再次删除它,我应该在那里声明服务吗?):

<service android:name="com.android.vending.billing.IInAppBillingService" />
4

1 回答 1

6

我遇到了同样的问题。您需要先消费已购买的商品,然后用户才能再次购买相同的 SKU 商品。

当您设置 buyIntentBundle 变量时,您可以检查 RESPONSE_CODE。如果 RESPONSE_CODE 为 7,那么用户之前的购买需要被应用消费。

RESPONSE_CODE ID 可在此处找到:http: //developer.android.com/google/play/billing/billing_reference.html#billing-codes

使用此示例代码查询购买的商品:http: //developer.android.com/google/play/billing/billing_integrate.html#QueryPurchases

INAPP_PURCHASE_DATA_LIST 中的数据是带有“purchaseToken”的 JSON 字符串。将此令牌传递给 CONSUME 函数。

一旦您消费了购买,用户就可以再次购买。

于 2013-12-28T16:19:01.040 回答