Android InApp,java.lang.NullPointerException 从 JsInterface 购买商品
Android InApp, java.lang.NullPointerException purchasing item from JsInterface
当用户通过 JsInterface
单击按钮时,我正尝试在我的 android 应用程序中购买商品。
方法 purchaseItem()
被执行,但是方法中的 launchPurchaseFlow()
没有被执行。你知道这个问题的解决方案吗?
OnCreate() 我用 IabHelper
执行 queryInventory
并且响应很好。
这里是我的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);
}
}
控制台 returns 这个 (java.lang.NullPointerException) :
06-05 12:09:05.219: E/JSInterface(9248): Purchase 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)
您正在使用 new 关键字创建 MainActivity。所以 mHelper 对象为 null 然后引发异常,您不能直接创建 MainActivity 对象。
MainActivity cls2= new MainActivity();// invalid object creation for activity.
cls2.purchaseItem(sku);
您的 Helper 变量在调用 launchPurchaseFlow() 方法之前返回 null。确保您的 mHelper 不为 null
改变这个:
MainActivity cls2= new MainActivity();
cls2.purchaseItem(sku);
收件人:
mContext.startActivity(new Intent(mContext, MainActivity.class).putExtra("item", sku));
然后,在 MainActivity
中的 OnCreate
中:
String item = getIntent().getStringExtra("item");
purchaseItem(item);
当用户通过 JsInterface
单击按钮时,我正尝试在我的 android 应用程序中购买商品。
方法 purchaseItem()
被执行,但是方法中的 launchPurchaseFlow()
没有被执行。你知道这个问题的解决方案吗?
OnCreate() 我用 IabHelper
执行 queryInventory
并且响应很好。
这里是我的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);
}
}
控制台 returns 这个 (java.lang.NullPointerException) :
06-05 12:09:05.219: E/JSInterface(9248): Purchase 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)
您正在使用 new 关键字创建 MainActivity。所以 mHelper 对象为 null 然后引发异常,您不能直接创建 MainActivity 对象。
MainActivity cls2= new MainActivity();// invalid object creation for activity.
cls2.purchaseItem(sku);
您的 Helper 变量在调用 launchPurchaseFlow() 方法之前返回 null。确保您的 mHelper 不为 null
改变这个:
MainActivity cls2= new MainActivity();
cls2.purchaseItem(sku);
收件人:
mContext.startActivity(new Intent(mContext, MainActivity.class).putExtra("item", sku));
然后,在 MainActivity
中的 OnCreate
中:
String item = getIntent().getStringExtra("item");
purchaseItem(item);