在基于龙与地下城帐单服务,bindService返回NullPointerException异常 [英] In Dungeons-based billing service, bindService returns NullPointerException

查看:224
本文介绍了在基于龙与地下城帐单服务,bindService返回NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现基于土牢样品账单服务(这是谷歌的方式推荐你去了解它)。它略由以下事实变得复杂:


  1. 主要服务类生活在一个库项目(因为我想reskin的code很多次)。由于这个StackOverflow的答案提出了在库项目可能会导致问题的服务,我所做出的服务抽象的继承了它在分项目;这也让我用不同的公钥对每个reskin(这是可取的)。

  2. 有很多UI元素的需要知道,如果计费是尽快支持越好。

因此​​,我调用一个函数来检查,如果计费是从第一个活动的run(实际上任何活动的的onCreate()法的支持,但preference然后将被写入所以这应该只发生一次)。

在地下城的样品中,code,以检查是否计费支持试图绑定到计费服务这样(从服务类):

 布尔bindResult = bindService(
    新意图(Consts.MARKET_BILLING_SERVICE_ACTION)
    对此,// ServiceConnection。
    Context.BIND_AUTO_CREATE);

这个调用抛出一个 NullPointerException异常。我仔细检查pretty并没有这些东西都是空;它发生的 的内部功能,显然在ContextWrapper.java的370线。

由于这个计算器其他答案我想知道,如果检查从的onCreate)计费(可能是premature但地下城示例项目,再次,从这里调用它,所以我不认为这是这一点。

我使用的是Android(3.1)晚得多的版本,这样可能会产生影响,但我很想知道这可能的原因。还有一件事:Eclipse是告诉我 customIntent 为空,但它并不完全清楚什么是指,我已经无法找到关于它的。

解决方案

答案原来是非常,非常简单:我错过了活动#的onCreate 此步骤:

  mBillingService =新BillingService有()//这一步是关键步骤
mBillingService.setContext(本);
//如果我早知道mBillingService.checkBillingSupported();

这要求 attachBaseContext(上下文);

现在的工作:)

I am implementing a billing service based on the Dungeons sample (which is the way Google recommend you go about it). It's slightly complicated by the following facts:

  1. The main service class lives in a library project (because I want to reskin the code many times). As this stackoverflow answer suggests a service in a library project may cause problems, I have made the service abstract and inherited it in the sub-projects; this also lets me use different public keys for each reskin (which is desirable).
  2. There are plenty of UI elements that need to know if billing is supported as soon as possible.

Consequently I am calling a function to check if billing is supported from the onCreate() method of the first activity run (actually any activity, but a preference will then be written so this should only happen once).

In the Dungeons sample, the code to check if billing is supported tries to bind to the billing service thus (from the Service class):

boolean bindResult = bindService(
    new Intent(Consts.MARKET_BILLING_SERVICE_ACTION),
    this,  // ServiceConnection.
    Context.BIND_AUTO_CREATE);

this call is throwing a NullPointerException. I've checked pretty carefully and none of these things are null; it's occurring within the function, apparently at line 370 of ContextWrapper.java.

Because of this other stackoverflow answer I wondered if checking billing from onCreate() might be premature but the Dungeons example project, again, calls it from here, so I don't think it's this.

I am using a much later version of Android (3.1) so that might have an effect, but I'd love to know the possible causes of this. One other thing: Eclipse is telling me customIntent is null, but it's not fully clear what that refers to and I've not been able to find much about it.

解决方案

The answer turns out to be very, very simple: I missed this step in Activity#onCreate:

mBillingService = new BillingService()

//THIS STEP IS THE CRITICAL STEP
mBillingService.setContext(this);
//IF ONLY I HAD KNOWN

mBillingService.checkBillingSupported();

This calls attachBaseContext(context);

It works now :)

这篇关于在基于龙与地下城帐单服务,bindService返回NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆