【问题标题】:IInAppBillingService always nullIInAppBillingService 始终为空
【发布时间】:2018-04-18 21:44:30
【问题描述】:

我正在尝试设置一个简单的应用内购买,但由于某种原因,当我尝试查询可用项目时,我总是得到一个空指针。我尝试使用Handler 来查看是否是等待问题,但这也不起作用,它只是阻止了代码运行。

public class AdOnsActivity extends AppCompatActivity {
    private IInAppBillingService mService;
    private ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad_ons);

        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);

        ArrayList<String> skuList = new ArrayList<String>();
        skuList.add(PRODUCT_ID_MORE_REPTILES);
        final Bundle querySkus = new Bundle();
        querySkus.putStringArrayList("ITEM_ID_LIST", skuList);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mService != null) {
                    //Query items here
                } else {
                    handler.postDelayed(this, 10);
                }
            }
        }, 10);

        buttonBuy = (Button) findViewById(R.id.buttonBuy);
        buttonBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), PRODUCT_ID_MORE_REPTILES, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                    PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
                    startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), 0, 0, 0);
                } catch (RemoteException|IntentSender.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 json = new JSONObject(purchaseData);
                    String sku = json.getString("productId");
                    Toast.makeText(this, "Congratulations on your purchase!", Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mService != null) {
            unbindService(mServiceConn);
        }
    }

【问题讨论】:

    标签: android google-play in-app-purchase in-app-billing


    【解决方案1】:

    堆栈跟踪以准确显示导致 NPE 的代码会很方便。但我会假设您的意思是 mService 在您的 onCreate 调用中始终为空。

    你本质上是在做糟糕的程序设计。获得可用的应用内产品是网络调用。而且您不应该在 onCreate 方法中进行异步网络调用。

    相反,为 IAP 填充一个空间。然后,当您获得 onServiceConnected() 时,触发listProducts() 调用。然后,当queryProducts() 得到结果时,填充 ListView 或您创建的任何内容。否则,对于始终离线的用户来说,您的体验会很糟糕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-31
      • 2019-07-18
      • 2019-08-18
      • 2011-12-29
      • 2019-07-02
      • 2019-05-04
      • 1970-01-01
      • 2011-05-04
      相关资源
      最近更新 更多