Android - 检查某些值不会

时间:2012-08-26 21:31:48

标签: java android

我正在进行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的东西吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

PurchaseState不是intlongfloat等原始数据类型,因此您应该使用:

purchaseState.equals(PurchaseState.PURCHASED)

而不是==,就像你使用字符串一样:

string1.equals(string2) // Results you expect

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