mService.getBuyIntent NullPointErexception

时间:2014-02-05 06:25:24

标签: android in-app

我目前在应用中遇到了问题

    bindService(new
            Intent("com.android.vending.billing.InAppBillingService.BIND"),
            mServiceConn, Context.BIND_AUTO_CREATE);

    String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFA....";

    mHelper = new IabHelper(this, base64EncodedPublicKey);
    mHelper.enableDebugLogging(true);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {

        @Override
        public void onIabSetupFinished(IabResult result) {
            // TODO Auto-generated method stub
            if(!result.isSuccess()) {

            }
            AlreadyPurchaseItems();             

        }
    });
}

public void AlreadyPurchaseItems() {
    try{
        Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
        int response = ownedItems.getInt("RESPONSE_CODE");
        Log.e("responseCode",Integer.toString(response));
        if(response == 0) {
            ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
            String[] tokens = new String[purchaseDataList.size()];
            for (int i = 0; i < purchaseDataList.size(); ++i)
            {
                String purchaseData = (String) purchaseDataList.get(i);
                JSONObject jo = new JSONObject(purchaseData);
                tokens[i] = jo.getString("purchaseToken");
                mService.consumePurchase(3,  getPackageName(), tokens[i]);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//Google Play Billing
IInAppBillingService mService;    
IabHelper mHelper;

ServiceConnection mServiceConn = new ServiceConnection() {
    @Override
    public void onServiceDisconnected(ComponentName name) {
        mService = null;
        Log.d("onServiceDisconnected","CALL!!!");
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = IInAppBillingService.Stub.asInterface(service);
        Log.d("onServiceConnected","CALL!!!");
    }
};    

public void BillBuyDia() {
    try {
        Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),  "zvscdia99", "inapp", "test1");


        PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");

        if(pendingIntent != null) {

            //OnIabPurchaseFinishedListener
            Log.d("INAPP","pendingIntent != NULL");             
            mHelper.launchPurchaseFlow(this, getPackageName(), 1001,  mPurchaseFinishedListener, "test1");
        } else {
            Log.d("INAPP","pendingIntent == NULL");
            AlreadyPurchaseItems();
        }

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

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase)
    {
        Log.d("INAPP","mPurchaseFinishedListener");
        AlreadyPurchaseItems();
    }
};


@Override
public void onDestroy() {
    super.onDestroy();
    if(mService != null) {
        Log.e("onDestroy","mService == NULL");
        unbindService(mServiceConn);
    }
}

mService.getBuyIntent (3, getPackageName (), "test", "inapp", "test1") error occurs Here NullPointException

在没有onServiceConnected

的情况下,还可以调用更多内容

我的Logcat

02-05 15:11:57.698: W/System.err(24097): java.lang.NullPointerException
02-05 15:11:57.703: W/System.err(24097):    at com.ProjectAlpha.necroprg.ProjectAlpha.BillBuyDia(ProjectAlpha.java:229)
02-05 15:11:57.703: W/System.err(24097):    at org.cocos2dx.lib.Cocos2dxRenderer.nativeTouchesEnd(Native Method)
02-05 15:11:57.703: W/System.err(24097):    at org.cocos2dx.lib.Cocos2dxRenderer.handleActionUp(Cocos2dxRenderer.java:129)

1 个答案:

答案 0 :(得分:0)

getActivity()。getPackageName()适合我