首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌计费V3空指针异常

谷歌计费V3空指针异常
EN

Stack Overflow用户
提问于 2015-07-14 09:20:09
回答 1查看 381关注 0票数 1

基本上,我想做一个静态的测试谷歌计费V3,之前做一个真正的在线测试使用Beta版本的东西。当我试图运行我的程序时,我得到了以下异常的一部分.

代码语言:javascript
复制
        Caused by: java.lang.IllegalArgumentException: connection is   
        null at android.app.ContextImpl.bindServiceCommon
        (ContextImpl.java:1935)
        at android.app.ContextImpl.bindService(ContextImpl.java:1921)
        at android.content.ContextWrapper.bindService
        (ContextWrapper.java:529)
        at victory.walkto.paymenttestb.MainActivity.
        onCreate(MainActivity.java:37)
        at android.app.Activity.performCreate(Activity.java:5451)

我编写的程序如下。

代码语言:javascript
复制
public class MainActivity extends Activity {

IInAppBillingService mService;
ServiceConnection connection;
String inappid = "android.test.purchased"; //replace this with your in-app product id

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    bindService(serviceIntent,connection, Context.BIND_AUTO_CREATE);
    connection = new ServiceConnection() {

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

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IInAppBillingService.Stub.asInterface(service);
        }
    };

    Button purchaseBtn = (Button) findViewById(R.id.purchase);
    purchaseBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ArrayList skuList = new ArrayList();
            skuList.add(inappid);
            Bundle querySkus = new Bundle();
            querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
            Bundle skuDetails;
            try {
                skuDetails = mService.getSkuDetails(3, getPackageName(),
                        "inapp", querySkus);

                int response = skuDetails.getInt("RESPONSE_CODE");
                if (response == 0) {

                    ArrayList<String> responseList = skuDetails
                            .getStringArrayList("DETAILS_LIST");

                    for (String thisResponse : responseList) {
                        JSONObject object = new JSONObject(thisResponse);
                        String sku = object.getString("productId");
                        String price = object.getString("price");
                        if (sku.equals(inappid)) {
                            System.out.println("price " + price);
                            Bundle buyIntentBundle = mService
                                    .getBuyIntent(3, getPackageName(), sku,
                                            "inapp",
                                            "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                            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 (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IntentSender.SendIntentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1001) {
        String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");

        if (resultCode == RESULT_OK) {
            try {
                JSONObject jo = new JSONObject(purchaseData);
                String sku = jo.getString(inappid);
                Toast.makeText(
                        MainActivity.this,
                        "You have bought the " + sku
                                + ". Excellent choice,adventurer!",
                        Toast.LENGTH_LONG).show();

            } catch (JSONException e) {
                System.out.println("Failed to parse purchase data.");
                e.printStackTrace();
            }
        }
    }
}
@Override
public void onDestroy() {
    super.onDestroy();
    if (connection != null) {
        unbindService(connection);
    }
 }
}

我还发现谷歌钱包不能在你的手机上下载,如果你使用的是希腊帐户!贝宝也因为银行问题而停止了与希腊的合作!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-14 09:26:04

您需要首先创建服务连接,然后绑定服务。

代码语言:javascript
复制
// first this
connection = new ServiceConnection() {

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

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = IInAppBillingService.Stub.asInterface(service);
    }
};

// then this
bindService(serviceIntent,connection, Context.BIND_AUTO_CREATE);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31402595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档