android - 从第二次 "In app purchase"在 Android 中抛出异常

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

我正在尝试将其包含在应用程序购买中,并且已成功显示可用的 SKU。现在我想进行虚假购买。所以我使用了 appId =“android.test.purchased”。这是它第一次完美地工作,但从接下来它会抛出异常,如下所示。

尝试在空对象引用上调用虚方法“android.content.IntentSender android.app.PendingIntent.getIntentSender()”

有人遇到过这种情况吗?

package com.inappbilling.poc;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.android.vending.billing.IInAppBillingService;

public class TestInAppPurchase extends Activity {
    private final static String SERVICE_INTENT = "com.android.vending.billing.InAppBillingService.BIND";
    private static final String _TAG = "BILLING ACTIVITY";

    private final String _testSku = "android.test.purchased";
    //available skus
    static final String SKU_7DAYS = "7days";
    static final String SKU_30DAYS = "30days";

    private Button _7daysPurchase = null;
    private Button _30daysPurchase = null;
    private IInAppBillingService _service = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(_TAG, "created");

        _7daysPurchase = ( Button ) findViewById(R.id.sevendays_btn);
        _30daysPurchase = ( Button ) findViewById(R.id.thirtydays_btn);
        _7daysPurchase.setOnClickListener(_purchaseListener);
        _30daysPurchase.setOnClickListener(_purchaseListener);

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    OnClickListener _purchaseListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch ( v.getId() ) {
            case R.id.sevendays_btn:
                doPurchase();
                break;
            case R.id.thirtydays_btn:
                break;
            }
        }
    };


    private ServiceConnection _serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {       
            _service = IInAppBillingService.Stub.asInterface( service );
            Log.d(_TAG, _service.toString());
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {     
            _service = null;
            Log.d(_TAG, "destroyed");
        }

    };

    private void doPurchase(){

        if ( _service == null) {
                Log.e( _TAG , "Billing failed: billing service is null ");
                return;
        }

        ArrayList testSku = new ArrayList( );
        testSku.add( _testSku );
        Bundle querySkus = new Bundle();
        querySkus.putStringArrayList("ITEM_ID_LIST", testSku);
        Bundle skuDetails ;
        try {
            skuDetails = _service.getSkuDetails(3, getPackageName(), "inapp", querySkus);
            int response = skuDetails.getInt("RESPONSE_CODE");
            if( response == 0 ){
                ArrayList<String> responseList = new ArrayList<String>( );
                responseList = skuDetails.getStringArrayList("DETAILS_LIST");
                for( String responseString : responseList ) {
                    JSONObject jobj = new JSONObject( responseString );
                    String sku = jobj.getString("productId");
                    if( sku.equals( _testSku )){
                         Bundle buyIntentBundle = _service.getBuyIntent(3, getPackageName(), sku ,"inapp","" );
                         PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
                         startIntentSenderForResult(pendingIntent.getIntentSender(),
                                 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
                                 Integer.valueOf(0));
                    }
                }
            } else {
                Log.e( _TAG , "Failed " );
            }
        } catch (Exception e) {
            Log.e( _TAG, "Caught exception  !"+ e.getMessage() );
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1001 ){
            String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
            if( resultCode == RESULT_OK ){
                try{
                    JSONObject jobj = new JSONObject( purchaseData );
                    String sku = jobj.getString(_testSku);
                    String paid = jobj.getString("price");
                    Log.v("SKU DATA", sku +"============"+ paid);

                }catch( Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if( _serviceConnection != null ){
            unbindService( _serviceConnection );
        }
    }
}

最佳答案

您好,请了解上述问题的解决方案。要解决此问题,您应该清除 Android 手机上 Google Play 应用的缓存。当我们进行任何应用内购买时,详细信息由 Google Play 维护服务。因此,一旦购买了产品,就隔离特定的 sku,以避免重复购买。 注意 - 这实际上是为了测试目的。因为 Google Play 服务会保留所有具有应用内购买或 Oauth 或任何其他设施的应用程序的记录。 希望它能帮助面临这个问题的人。

关于android - 从第二次 "In app purchase"在 Android 中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21932574/

相关文章:

android - 无法在 Glide 中显示远程 svg 文件

android - 什么会导致 getSkuDetails() 返回 null?

ios - 应用内购买视频

java - 跨 session /设备处理应用内购买/消耗品?

Android 订阅 getPurchases

android - 在我的应用程序运行时监听 PURCHASES_UPDATED 以兑换促销代码

android - 如何删除保存在模拟器 SDCard 上的文件?

java - Arraylist 不适用于 Android

android - 在没有自定义 ListView 的 ListActivity 中使用 setDivider 更改分隔线?

iphone - 如何从交易队列中删除非常旧的、已失效的应用内购买