android – 在startIntentSenderForResult中的NullPointer,不适用V3

我有以下方法(第一次工作正常):
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));
    } 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是null(虽然奇怪的是,它工作了一次,所以看起来几乎像一个问题,如果你不止一次购买相同的项目).我有以下内容

这是我的代码

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

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

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

解决方法

我遇到了同样的问题.在用户可以再次购买相同的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函数.

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...