我試圖在用戶通過JsInterface
單擊某個按鈕時在我的Android應用中購買商品。Android InApp,java.lang.NullPointerException從JsInterface購買商品
方法purchaseItem()
被執行但是launchPurchaseFlow()
這個方法裏面沒有。你知道這個問題的解決方案嗎?
OnCreate()我執行queryInventory
與IabHelper
和響應就好了。
這裏是我的MainActivity的購買部分的部分代碼,強行android.test.purchased,並主要從TrivialDrive樣本複製:
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d("PURCHASE", "Purchase finished: " + result + ", purchase: " + purchase);
if (mHelper == null) return;
if (result.isFailure()) {
Log.d("PURCHASE","Error purchasing: " + result);
return;
}
Log.d("PURCHASE", "Purchase successful.");
if (purchase.getSku().equals("android.test.purchased")) {
Log.d(TAG, "Purchase is gas. Starting gas consumption.");
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
Log.d("CONSUME", "Consumption finished. Purchase: " + purchase + ", result: " + result);
if (mHelper == null) return;
if (result.isSuccess()) {
Log.d("CONSUME", "Consumption successful. Provisioning.");
Log.d("CONSUME","You bought gas");
}
else {
Log.d("CONSUME","Error while consuming: " + result);
}
Log.d("CONSUME", "End consumption flow.");
}
};
public void purchaseItem(String sku_item) {
Log.d("PURCHASE", "Buy "+sku_item);
String payload = "";
mHelper.launchPurchaseFlow(this, "android.test.purchased", 1001, mPurchaseFinishedListener, payload);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 1001) {
int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString("productId");
Log.d("PURCHASE RESULT","You have bought the " + sku + ". Excellent choice, adventurer!");
}
catch (JSONException e) {
Log.d("PURCHASE RESULT","Failed to parse purchase data.");
e.printStackTrace();
}
}
}
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
這裏是我的JsInterface
:
public class jsinterface {
Context mContext;
jsinterface(Context c) { mContext = c; }
@JavascriptInterface
public void purchaseSku(String sku) {
Log.e("JSInterface","Purchase SKU : " + sku);
MainActivity cls2= new MainActivity();
cls2.purchaseItem(sku);
}
}
控制檯返回這個(顯示java.lang.NullPointerException):
一十二月6日至5日:09:0 5.219:E/JSInterface(9248):購買SKU:android.test.purchased 06-05 12:09:05.219:D/PURCHASE(9248):Buy android.test.purchased 06-05 12:09:05.219: W/System.err(9248):java.lang.NullPointerException 06-05 12:09:05.219:W/System.err(9248):at com.mcrae.confess.MainActivity.purchaseItem(MainActivity.java:208) 06-05 12:09:05.219:W/System.err(9248):at com.mcrae.confess.jsinterface.purchaseSku(jsinterface.java:27) 06-05 12:09:05.219:W/System。 err(9248):at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) 06-05 12:09:05.219:W/System.err(9248):at com.android.org.chromium .base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27) 06-05 12:09:05.219:W/System.err(9248):at android.os.Handler.dispatchMessage(Handler。 java:102) 06-05 12:09:05.219:W/System.err(9248):at android.os.Looper.loop(Looper.java:146) 06-05 12:09:05.219:W/System.err(9248):at android.os.HandlerThread.run(HandlerThread.java:61)
謝謝,這是對的解決方案 –