【发布时间】:2015-08-17 17:34:06
【问题描述】:
我制作了一个适用于播放服务的应用程序。我在 HTC 310、nexus、lenovo p780 和其他一些设备上测试了我的应用程序。它根本不会崩溃。我已经安装了 Crashlytics 并收到了一些奇怪的崩溃报告:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at com.preferences.iaps.IabHelper.queryPurchases(IabHelper.java:829)
at com.preferences.iaps.IabHelper.queryInventory(IabHelper.java:557)
at com.preferences.iaps.IabHelper.queryInventory(IabHelper.java:521)
at com.preferences.iaps.IabHelper$2.run(IabHelper.java:614)
at java.lang.Thread.run(Thread.java:818)
我从 s4、s5 等三星收到此错误。这些设备有问题还是应用程序有问题?
这是我调用并将上下文传递给 Iabhelper 的示例:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mHelper = new IabHelper(mActivity, base64EncodedPublicKey);
mHelper.enableDebugLogging(true);
mHelper.startSetup(this);
}
这是导致抛出空指针异常的原因:(第 829 行):
logDebug("Package name: " + mContext.getPackageName());
这是我获取上下文的地方(错误所在的同一类)
public IabHelper(Context ctx, String base64PublicKey) {
mContext = ctx.getApplicationContext();
mSignatureBase64 = base64PublicKey;
logDebug("IAB helper created.");
}
【问题讨论】:
-
也许发布将 NullPointer 放到那里的实际代码。
-
@adminral : mContext 突然从何而来?什么是 logDebug 功能?请发布解决方案的相关代码。
-
如果您传递的是
Context,那么为什么要mContext = ctx.getApplicationContext();?为什么不mContext = ctx;?你很少需要getApplicationContext()。尤其是当您已经通过Context
标签: android android-activity android-context