Test In-App-Purchase in Codename One simulator results in Null Pointer Exception

戏子无情 提交于 2019-12-07 00:45:38

The simulator doesn't connect to google play and can return null for things you expect to be there. Notice that there are API's that indicate things such as if item listing is supported etc.

You can manipulate some elements of the purchase API via the simulator menu.

So to get rid of the NPE that appears right after the purchase() method returns in the simulator, the main class MUST implement PurchaseCallback interface!!!

The Main CN1 class is the one with the start() and init() methods not the state machine as [indicated by Shai][2]. This is a crucial part and should be stressed in the [Purchase tutorial][3] although this is well written in the API documentation :

public interface PurchaseCallback Callback interface that the main class must implement in order for in-app-purchasing to work. Once the main class implements this interface the methods within it are invoked to indicate the various purchase states.

Now the itemPurchased() method from the PurchaseCallback implementation gets called when the after purchase() returns. And the simulator keeps track of the purchase in the CN1InAppPurchases file (.cn1 folder on Linux).

Furthermore now that the PurchaseCallback has been implemented, in the simulator (not on the Android device however) the wasPurchased() method returns the expected value depending on whether or not a previous purchase() call succeeded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!