2017-08-15 141 views
2

我与有例外Google Play结算库 v3。 我在onDestroy活动方法释放帐单客户端。Android结算v3 - BillingClientImpl.java:668处的NullPointerExceptionException

有没有人有此问题?

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 
    at com.android.billingclient.api.BillingClientImpl$BillingServiceConnection.onServiceConnected(BillingClientImpl.java:668) 
    at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1259) 
    at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1276) 
    at android.os.Handler.handleCallback(Handler.java:815) 
    at android.os.Handler.dispatchMessage(Handler.java:104) 
    at android.os.Looper.loop(Looper.java:224) 
    at android.app.ActivityThread.main(ActivityThread.java:5830) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879) 

计费客户端连接调用的代码:

private com.android.billingclient.api.BillingClient billingClient; 

public AppBillingManager(Context context) { 
    billingClient = new BillingClient.Builder(context) 
      .setListener(this) 
      .build(); 
} 

public void doConnect(final Runnable postConnection) { 
    billingClient.startConnection(new BillingClientStateListener() { 
     @Override 
     public void onBillingSetupFinished(int resultCode) { 
      isConnected = BillingClient.BillingResponse.OK == resultCode; 
      notifyConnectivityState(); 
      if (isConnected) { 
       postConnection.run(); 
      } else { 
       listener.onConnectedToBillingService(resultCode); 
      } 
     } 

     @Override 
     public void onBillingServiceDisconnected() { 
      isConnected = false; 
      listener.onDisconnectedBillingService(); 
     } 
    }); 
} 

public void release() { 
    this.billingClient.endConnection(); 
} 
+1

在低端设备上面临同样的问题。 – pratt

+0

@pratt你有没有找到解决方案,我现在正面临这个问题,无法理解NPE来自 – spaceman

+0

@spaceman,NPE来自谷歌计费库。为了解决这个问题,我简单地将billing-library项目作为模块添加到我自己的项目中,并编辑BillingClientImpl.java源文件。问题在于调用BillingManager.destroy()(通常在Activity.onDestroy()中)会调用BillingClientImpl.endConnection(),从而导致内部mContext引用无效。但是在某些情况下,您仍然可以在活动死亡后调用BillingServiceConnection.onServiceConnected(),并在内部使用已经为null的mContext。您可以选择不要使mContext无效或将其检查为空。 – pratt

回答