Android InApp,java.lang.NullPointerException从JsInterface购买项目

时间:2015-06-05 10:31:42

标签: android in-app-purchase in-app-billing in-app android-jsinterface

当用户点击JsInterface按钮时,我试图在我的Android应用中购买商品。

方法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);
  }

 }

Console返回此(java.lang.NullPointerException):

  

06-05 12:09:05.219:E / JSInterface(9248):购买SKU:android.test.purchased   06-05 12:09:05.219:D / PURCHASE(9248):买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):在android.os.Handler.dispatchMessage(Handler.java:102)   06-05 12:09:05.219:W / System.err(9248):在android.os.Looper.loop(Looper.java:146)   06-05 12:09:05.219:W / System.err(9248):在android.os.HandlerThread.run(HandlerThread.java:61)

3 个答案:

答案 0 :(得分:1)

改变这个:

MainActivity cls2= new MainActivity();
cls2.purchaseItem(sku);

致:

mContext.startActivity(new Intent(mContext, MainActivity.class).putExtra("item", sku));

然后,在OnCreateMainActivity

String item = getIntent().getStringExtra("item");
purchaseItem(item);

答案 1 :(得分:0)

您正在使用new关键字创建MainActivity。因此,mHelper对象为null然后异常引发,您无法直接创建MainActivity对象。

MainActivity cls2= new MainActivity();// invalid object creation for activity.
cls2.purchaseItem(sku);

答案 2 :(得分:0)

你的mHelper变量在调用launchPurchaseFlow()方法之前返回null。确保您的mHelper不为空