0

ユーザーがボタンをクリックしたときに、Android アプリでアイテムを購入しようとしていますJsInterface

メソッドpurchaseItem()は実行されますが、launchPurchaseFlow()このメソッド内では実行されません。この問題の解決策を知っていますか?

OnCreate() with を実行するqueryInventoryIabHelper、応答は問題ありません。

これは、私の 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) :

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): com.mcrae.confess.MainActivity.purchaseItem(MainActivity. java:208) 06-05 12:09:05.219: W/System.err(9248): com.mcrae.confess.jsinterface.purchaseSku (jsinterface.java:27) 06-05 12:09:05.219: W/ System.err(9248): com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(ネイティブ メソッド) 06-05 12:09:05.219: W/System.err(9248): 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: システムあり。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)

4

3 に答える 3

1

これを変える:

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

に :

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

そして、あなたのOnCreatein でMainActivity

String item = getIntent().getStringExtra("item");
purchaseItem(item);
于 2015-06-05T10:45:31.733 に答える
0

You are creating the MainActivity using new keyword. So the mHelper object is null then exception raises, you cannot create MainActivity object directly.

MainActivity cls2= new MainActivity();// invalid object creation for activity.
cls2.purchaseItem(sku);
于 2015-06-05T10:39:45.020 に答える
0

Your mHelper variable is returning null before it calles the launchPurchaseFlow() method. Make sure your mHelper is not null

于 2015-06-05T10:39:54.003 に答える