我正在处理Android帐单,并尝试正确设置onPurchaseStateChange方法,但是它似乎无法正常工作。

    @Override
    public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
            int quantity, long purchaseTime, String developerPayload)
    {
        if (purchaseState == PurchaseState.PURCHASED)
        {
            mOwnedItems.add(itemId);

            if ( itemId != null && itemId.trim().equals("3") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);
            }
            if ( itemId != null && itemId.trim().equals("4") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);
            }
        }
        else
        if (purchaseState == PurchaseState.CANCELED)
        {
            // purchase canceled
        }
        else
        if (purchaseState == PurchaseState.REFUNDED)
        {
            // user ask for a refund
        }
        else
        {
            if ( itemId != null && itemId.equals("3") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);
            }
            if ( itemId != null && itemId.equals("4") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);
            }
        }


因此,当purchaseId为“ 4”或“ 3”且purchaseState == PurchaseState.PURCHASED ....出于某种原因,它似乎未进入该if语句,并且不执行进入下一页的意图?

谁知道为什么会这样吗?看起来很奇怪。难道是Java的事情吗?

谢谢!

最佳答案

PurchaseState不是类似于intlongfloat等的原始数据类型,因此应使用:

purchaseState.equals(PurchaseState.PURCHASED)


代替==,就像您使用Strings一样:

string1.equals(string2) // Results you expect


与...不同

string1 == string2 // Don't do this...

关于java - Android-检查某些值不,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12134079/

10-09 07:51