2013-03-08 33 views
0

我在我的代码得到一个零点错误和日志在这里指点为什么我在我的Android代码中出现空指针错误?

199  public void getPurchasedSubs() throws RemoteException { 
200   Bundle ownedItems = mService.getPurchases(3, getPackageName(), "subs", null); 

这里是logcat的文件转储

03-08 13:24:45.619: E/AndroidRuntime(7149): FATAL EXCEPTION: main 
03-08 13:24:45.619: E/AndroidRuntime(7149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.imobilize.ETV_V3_INAPP_SUB/com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity}: java.lang.NullPointerException 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.app.ActivityThread.access$600(ActivityThread.java:128) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.os.Looper.loop(Looper.java:137) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.app.ActivityThread.main(ActivityThread.java:4517) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at dalvik.system.NativeStart.main(Native Method) 
03-08 13:24:45.619: E/AndroidRuntime(7149): Caused by: java.lang.NullPointerException 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.getPurchasedSubs(IntroBLevActivity.java:200) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.checkValidation(IntroBLevActivity.java:238) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.onCreate(IntroBLevActivity.java:96) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.app.Activity.performCreate(Activity.java:4470) 
03-08 13:24:45.619: E/AndroidRuntime(7149):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) 

SPECS此方法

/** 
* Returns the current SKUs owned by the user of the type and package name specified along with 
* purchase information and a signature of the data to be validated. 
* This will return all SKUs that have been purchased in V3 and managed items purchased using 
* V1 and V2 that have not been consumed. 
* @param apiVersion billing API version that the app is using 
* @param packageName package name of the calling app 
* @param type the type of the in-app items being requested 
*  ("inapp" for one-time purchases and "subs" for subscription). 
* @param continuationToken to be set as null for the first call, if the number of owned 
*  skus are too many, a continuationToken is returned in the response bundle. 
*  This method can be called again with the continuation token to get the next set of 
*  owned skus. 
* @return Bundle containing the following key-value pairs 
*   "RESPONSE_CODE" with int value, RESULT_OK(0) if success, other response codes on 
*    failure as listed above. 
*   "INAPP_PURCHASE_ITEM_LIST" - StringArrayList containing the list of SKUs 
*   "INAPP_PURCHASE_DATA_LIST" - StringArrayList containing the purchase information 
*   "INAPP_DATA_SIGNATURE_LIST"- StringArrayList containing the signatures 
*          of the purchase information 
*   "INAPP_CONTINUATION_TOKEN" - String containing a continuation token for the 
*          next set of in-app purchases. Only set if the 
*          user has more owned skus than the current list. 
*/ 
Bundle getPurchases(int apiVersion, String packageName, String type, String continuationToken); 

当我检查它的包装名称是否有效

她e是创建mService的代码 IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() { 
    //@Overide 
     public void onServiceDisconnected(ComponentName name) { 
     mService = null; 
    } 
     //@Overide 
    public void onServiceConnected(ComponentName name, 
     IBinder service) { 
     mService = IInAppBillingService.Stub.asInterface(service); 
    } 

}; 

我不擅长解释logcat文件,所以答案可能就在我面前。

这段代码我认为应该打开它,所以我随时使用mService。

 bindService(new 
      Intent("com.android.vending.billing.InAppBillingService.BIND"), 
        mServiceConn, Context.BIND_AUTO_CREATE); 
+2

您是否尝试过使用断点并逐步执行代码?当您尝试调用getPurchases()时,似乎mService为null。 – crazylpfan 2013-03-08 21:40:49

+0

将日志添加到您的onServiceDisconnected并查看它是否在执行代码之前断开连接。 还要在异常语句之前添加空检查并记录以了解其是否为空。 我怀疑可能是您的服务在您执行此代码时未连接,或者您在执行此代码时断开连接 – anargund 2013-03-08 21:41:42

+0

哪条线路导致NPE? (ActivityThread.java:1970) – 2013-03-09 00:58:14

回答

4

我想你在使用mService连接到服务之前。

我的建议是使用布尔变量来检查活动连接服务与否, 或onServiceConnected使用getPurchasedSubs()这样的:

ServiceConnection mServiceConn = new ServiceConnection() { 
    public void onServiceDisconnected(ComponentName name) { 
     mService = null; 
    } 

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

}; 

希望这有助于你。

+0

我该如何制作get PurchasedSubs();在我需要的时候随时可用。我添加了一块我的onCreate,它应该使这个可用,但它不,并且这是它为什么不工作的一部分 – 2013-03-09 00:43:16

+0

另外,当我处理MServiceeConn的OVERIDE部分时,我得到一个奇怪的错误,它说我需要删除它,因为必须重写超类方法,所以我删除它。这可能会有一个不好的影响? – 2013-03-09 00:55:47

+0

您可以使用PurchasedSubs();任何时候你想要的,但必须在连接到服务之后。你也可以把bindService(...);在onStart()在onCreate execute之前绑定它。请参阅此链接[---链接---](http://dharmendra4android.blogspot.com/2012/05/bind-service-using-ibinder-class.html) – AwadKab 2013-03-09 03:07:09

相关问题