2013-03-08 24 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

相關問題