【问题标题】:Google Billing V3 null pointer exceptionGoogle Billing V3 空指针异常
【发布时间】:2015-07-14 09:20:09
【问题描述】:

基本上,我想先对 Google Billing V3 进行静态测试,然后再使用 Beta 版进行真正的在线测试。当我尝试运行我的程序时,我得到以下异常部分...

        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)

我写的程序如下。

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);
    }
 }
}

我还发现如果您使用的是希腊帐户,则无法在您的手机中下载谷歌钱包!!!由于银行问题,Paypal 也停止了在希腊的业务!!!

【问题讨论】:

    标签: android exception billing


    【解决方案1】:

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

    // 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);
    

    【讨论】:

    • 谢谢伙计。测试版本正在运行。我想问你点别的。我应该使用相同的有效负载字符串进行真正的在线测试吗?还是我应该生成一个新的?最后,我应该在 String inappid 中放入什么?我的应用程序包名称后跟 productid(开发者控制台中添加产品的标识符)?即胜利.walkto.paymenttestb.productid
    • inappid 和 productId 是相同的字符串 - 您在开发控制台中定义的应用内产品的 id。第一个(inappid)用于查询有关此产品 id 的信息。如果您有多个产品,您将在列表中添加多个 inappid。第二个(productId)用于从响应中获取具有正确 id 的产品。仅当您有多种产品时才有意义。如果只有一种产品,响应将仅包含该产品的详细信息。
    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2015-12-17
    • 2015-07-12
    相关资源
    最近更新 更多