我在Service中创建了一个IabHelper对象,但有时它会抛出一个NullPointerException,我发现这是由于context.getApplicationContext()
有时会在IabHelper的构造函数中返回null
。
换句话说,context.getApplicationContext()
有时会返回null
,但我无法理解原因。
IabHelper对象是here描述的对象,略有修改。
这就是我所做的:在Service的onCreate()方法中,构造一个IabHelper对象:
public class MyService extends Service {
IabHelper helper;
...
public void onCreate() {
super.onCreate();
...
helper = new IabHelper(this);
...
}
}
此处this
不应为null。 IabHelper对象是这样的:
public class IabHelper {
Context context;
public IabHelper(Context ctx) {
context = ctx.getApplicationContext();
...
}
...
}
然后,如上所述,当我调用context.getPackageName()
或任何其他方法时,会发出NullPointerException。这个问题在一个月内仅发生了大约50次用户(事实上我无法重现)。
我发现的唯一类似问题是this one,但我没有在另一个进程中运行服务,所以不是我的情况。
有什么想法吗?提前谢谢。